<p>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.</p>

<pre><code>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"))));
</code></pre>

<p>The result is ...</p>

<pre><code>&lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;
  &lt;url xmlns=""&gt;
    &lt;loc&gt;http://www.example.com/page&lt;/loc&gt;
    &lt;lastmod&gt;2008-09-14&lt;/lastmod&gt;
  &lt;/url&gt;
&lt;/urlset&gt;
</code></pre>

<p>I would rather not have the xmlns="" on the url element. I can strip it out using Replace on the final xdoc.ToString(), but is there a more correct way?</p>

[{'answer_id': 61106, 'author': 'Anheledir', 'author_id': 5703, 'author_profile': 'https://Stackoverflow.com/users/5703', 'pm_score': 2, 'selected': False, 'text': '<p>If one element uses a namespace, they all must use one. In case you don\'t define one on your own the framework will add a empty namespace as you have noticed. And, sadly, there is no switch or something similiar to suppress this "feature".</p>\n\n<p>So, there seems to be no better method as to strip it out. Using <em>Replace(" xmlns=\\"\\"", "")</em> could be a little bit faster than executing a RegEx.</p>\n'}, {'answer_id': 61141, 'author': 'Micah', 'author_id': 6209, 'author_profile': 'https://Stackoverflow.com/users/6209', 'pm_score': 6, 'selected': True, 'text': '<p>The "more correct way" would be:</p>\n\n<pre><code>XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "true"),\nnew XElement(ns + "urlset",\nnew XElement(ns + "url",\n    new XElement(ns + "loc", "http://www.example.com/page"),\n    new XElement(ns + "lastmod", "2008-09-14"))));\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>&lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;\n  &lt;url&gt;\n    &lt;loc&gt;http://www.example.com/page&lt;/loc&gt;\n    &lt;lastmod&gt;2008-09-14&lt;/lastmod&gt;\n  &lt;/url&gt;\n&lt;/urlset&gt;\n</code></pre>\n\n<p>which is, if I\'m not mistaken, what you want.</p>\n'}, {'answer_id': 777327, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I stumbled across this post while dealing with a similar problem in VB.NET.  I was using XML literals and it took me some searching to figure out how to make this solution work with the XML literal construction and not just the functional construction.</p>\n\n<p>The solution is to import the XML namespace at the top of the file.</p>\n\n<pre><code>Imports &lt;xmlns:ns="x-schema:tsSchema.xml"&gt;\n</code></pre>\n\n<p>And then prefix all of my XML literals in the query expression with the imported namespace.  This removes the empty namespace that were appearing on the  elements when I saved my output.</p>\n\n<pre><code>Dim output As XDocument = &lt;?xml version="1.0" encoding="utf-8"?&gt;\n                              &lt;XML ID="Microsoft Search Thesaurus"&gt;\n                                  &lt;thesaurus xmlns="x-schema:tsSchema.xml"&gt;\n                                      &lt;diacritics_sensitive&gt;0&lt;/diacritics_sensitive&gt;\n                                      &lt;%= From tg In termGroups _\n                                          Select &lt;ns:expansion&gt;\n                                                     &lt;%= From t In tg _\n                                                         Select &lt;ns:sub&gt;&lt;%= t %&gt;&lt;/ns:sub&gt; %&gt;\n                                                 &lt;/ns:expansion&gt; %&gt;\n                                  &lt;/thesaurus&gt;\n                              &lt;/XML&gt;\n\n    output.Save("C:\\thesaurus.xml")\n</code></pre>\n\n<p>I hope this helps someone.  Despite bumps in the road like this, the XLinq API is pretty darn cool.</p>\n'}]
['https://Stackoverflow.com/questions/61084', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4449/']