.NET 8 C# Sitemap parser and Sitemap Index parser.
PM> Install-Package Toimik.SitemapsProtocol
> dotnet add package Toimik.SitemapsProtocol
This example shows how to use the Sitemap
class.
Usage of the SitemapIndex
class is similar.
// The value that every sitemap entry's location must start with
// in order for that entry to be considered as valid
var location = new Uri("http://www.example.com");
// Create an instance of the Sitemap class
var sitemap = new Sitemap(location);
// Load the data from a Stream
using var stream = // Create a local or remote stream
await sitemap.Load(stream);
// Or load the data from a string
// var data = "...";
// sitemap.Load(data);
...
var entries = sitemap.Entries;
Console.WriteLine($"{sitemap.EntryCount} entries parsed:");
// Enumerate the sitemap entries
while (entries.MoveNext())
{
var entry = entries.Current;
Console.WriteLine(entry.Location);
...
}
This example shows how to use the SitemapParser
class.
Usage of the SitemapIndexParser
class is similar.
var location = new Uri("http://www.example.com");
var parser = new SitemapParser(location);
using var stream = // Create a local or remote stream
await foreach (SitemapEntry entry in parser.Parse(stream))
{
...
}
Internally, Sitemap
and SitemapIndex
use SitemapParser
and SitemapIndexParser
, respectively.