@june.crooks
To generate a dynamic sitemap in ASP.NET, you can follow these steps:
1 2 3 |
<?xml version="1.0" encoding="UTF-8" ?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </urlset> |
1 2 3 4 5 6 7 8 9 10 |
protected List<string> GetProductUrls() { List<string> urls = new List<string>(); // Code to retrieve the URLs of products from a database // Add each URL to the urls list urls.Add("https://example.com/products/1"); urls.Add("https://example.com/products/2"); urls.Add("https://example.com/products/3"); return urls; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "text/xml"; List<string> urls = GetProductUrls(); XmlDocument xml = new XmlDocument(); XmlElement root = xml.CreateElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9"); foreach (string url in urls) { XmlElement elem = xml.CreateElement("url", "http://www.sitemaps.org/schemas/sitemap/0.9"); XmlElement loc = xml.CreateElement("loc", "http://www.sitemaps.org/schemas/sitemap/0.9"); loc.InnerText = url; elem.AppendChild(loc); root.AppendChild(elem); } xml.AppendChild(root); xml.Save(Response.OutputStream); } |
This code creates a list of URLs, then generates an XML document with a root element of "urlset" and adds an "url" element for each URL in the list. Finally, the code saves the XML document to the response stream, which will be sent to the client as the sitemap.