How to generate xsi:schemalocation attribute correctly when generating a dynamic sitemap.xml with LINQ to XML?
Asked Answered
B

2

6

I am generating a dynamic sitemap.xml

According to sitemaps.org this is the header for a sitemap.xml

<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    ...
    </url>
</urlset>

So I'm using LINQ To XML to generate the sitemap.xml

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
return new XElement(ns + "urlset",
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    //new XAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

The commented line is the one i cannot get right.
How can i set the "xsi:schemalocation" attribute?

Thanks.

Brathwaite answered 27/7, 2010 at 18:2 Comment(0)
B
4

Ok, I got it right. Thanks to Mike Caron
If I declare the XAtrribute(XNamespace.Xmlns + "xsi",...) then it works

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
return new XElement(ns + "urlset",  
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in GetNodes() 
    select new XElement(ns + "url", 
        new XElement(ns + "loc", node.Loc), 
        new XElement(ns + "lastmod", node.LastMod), 
        new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 
Brathwaite answered 27/7, 2010 at 19:4 Comment(5)
To be fair, it doesn't matter one bit what the actual namespace identifer is. It could be "carlosmunoz" for all it matters :)Buoyage
Cool, then I'll use "carlosmunoz"Zetes
Hey Carlos, what does the 'GetNodes' method return? Thanks in advance.Deglutinate
@Ethan, GetNodes() returns a List<SitemapNode>. SiteMapNode is a class with 3 properties: string Loc (Url of the page), DateTime Lastmod (last modified timestamp of the url) and double Priority (priority of the url in relation to others)Zetes
Thank you. I found answer on my question about xmlns:xsiSavoie
B
3

I don't know LINQ to XML, but after a quick peek at the documentation, try this:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
return new XElement(ns + "urlset",
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

Note that I'm not setting the xmlns attributes explicitly. I suspect they're generated automatically. Also, caveat emptor, since this is not tested.

Buoyage answered 27/7, 2010 at 18:16 Comment(1)
This almost works but it generates :p1 instead of :xsi since the string "xsi" is not set anywhere.Zetes

© 2022 - 2024 — McMap. All rights reserved.