Empty namespace using Linq Xml

后端 未结 3 629
悲&欢浪女
悲&欢浪女 2020-12-14 15:11

I\'m trying to create a sitemap using Linq to Xml, but am getting an empty namespace attribute, which I would like to get rid of. e.g.

XNamespace ns = \"http         


        
3条回答
  •  再見小時候
    2020-12-14 15:51

    The "more correct way" would be:

    XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
    new XElement(ns + "urlset",
    new XElement(ns + "url",
        new XElement(ns + "loc", "http://www.example.com/page"),
        new XElement(ns + "lastmod", "2008-09-14"))));
    

    Same as your code, but with the "ns +" before every element name that needs to be in the sitemap namespace. It's smart enough not to put any unnecessary namespace declarations in the resulting XML, so the result is:

    
      
        http://www.example.com/page
        2008-09-14
      
    
    

    which is, if I'm not mistaken, what you want.

提交回复
热议问题