Empty namespace using Linq Xml -
i'm trying create sitemap using linq xml, getting empty namespace attribute, rid of. e.g.
xnamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; xdocument xdoc = new xdocument(new xdeclaration("1.0", "utf-8", "true"), new xelement(ns + "urlset", new xelement("url", new xelement("loc", "http://www.example.com/page"), new xelement("lastmod", "2008-09-14"))));
the result ...
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url xmlns=""> <loc>http://www.example.com/page</loc> <lastmod>2008-09-14</lastmod> </url> </urlset>
i rather not have xmlns="" on url element. can strip out using replace on final xdoc.tostring(), there more correct way?
the "more correct way" 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 code, "ns +" before every element name needs in sitemap namespace. it's smart enough not put unnecessary namespace declarations in resulting xml, result is:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/page</loc> <lastmod>2008-09-14</lastmod> </url> </urlset>
which is, if i'm not mistaken, want.
Comments
Post a Comment