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

后端 未结 2 1267
鱼传尺愫
鱼传尺愫 2021-02-09 21:44

I am generating a dynamic sitemap.xml

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


<         


        
2条回答
  •  情书的邮戳
    2021-02-09 22:28

    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.

提交回复
热议问题