<p>I use .NET XML technologies quite extensively on my work. One of the things the I like very much is the XSLT engine, more precisely the extensibility of it. However there one little piece which keeps being a source of annoyance. Nothing major or something we can't live with but it is preventing us from producing the beautiful XML we would like to produce.  </p>

<p>One of the things we do is transform nodes inline and importing nodes from one XML document to another. </p>

<p>Sadly , when you save nodes to an <code>XmlTextWriter</code> (actually whatever <code>XmlWriter.Create(Stream)</code>  returns), the namespace definitions get all thrown in there, regardless of it is necessary (previously defined) or not. You get kind of the following xml:</p>

<pre><code>&lt;root xmlns:abx="http://bladibla"&gt;  
     &lt;abx:child id="A"&gt;  
         &lt;grandchild id="B"&gt;
             &lt;abx:grandgrandchild xmlns:abx="http://bladibla" /&gt;  
         &lt;/grandchild&gt;  
     &lt;/abx:child&gt;  
&lt;/root&gt;
</code></pre>

<p>Does anyone have a suggestion as to how to convince .NET to be efficient about its namespace definitions?</p>

<p>PS. As an added bonus I would like to override the default namespace, changing it as I write a node.</p>

[{'answer_id': 6421794, 'author': 'Simon Mourier', 'author_id': 403671, 'author_profile': 'https://Stackoverflow.com/users/403671', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not sure this is what you\'re looking for, but you can use this kind of code when you start writing to the Xml stream:</p>\n\n<pre><code>myWriter.WriteAttributeString("xmlns", "abx", null, "http://bladibla");\n</code></pre>\n\n<p>The XmlWriter should remember it and not rewrite it anymore. It may not be 100% bulletproof, but it works most of the time.</p>\n'}, {'answer_id': 6482730, 'author': 'Kirill Polishchuk', 'author_id': 787016, 'author_profile': 'https://Stackoverflow.com/users/787016', 'pm_score': 4, 'selected': False, 'text': '<p>Use this code:</p>\n\n\n\n<pre class="lang-cs prettyprint-override"><code>using (var writer = XmlWriter.Create("file.xml"))\n{\n    const string Ns = "http://bladibla";\n    const string Prefix = "abx";\n\n    writer.WriteStartDocument();\n\n    writer.WriteStartElement("root");\n\n    // set root namespace\n    writer.WriteAttributeString("xmlns", Prefix, null, Ns);\n\n    writer.WriteStartElement(Prefix, "child", Ns);\n    writer.WriteAttributeString("id", "A");\n\n    writer.WriteStartElement("grandchild");\n    writer.WriteAttributeString("id", "B");\n\n    writer.WriteElementString(Prefix, "grandgrandchild", Ns, null);\n\n    // grandchild\n    writer.WriteEndElement();\n    // child\n    writer.WriteEndElement();\n    // root\n    writer.WriteEndElement();\n\n    writer.WriteEndDocument();\n}\n</code></pre>\n\n<p>This code produced desired output:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;root xmlns:abx="http://bladibla"&gt;\n  &lt;abx:child id="A"&gt;\n    &lt;grandchild id="B"&gt;\n      &lt;abx:grandgrandchild /&gt;\n    &lt;/grandchild&gt;\n  &lt;/abx:child&gt;\n&lt;/root&gt;\n</code></pre>\n'}, {'answer_id': 6493730, 'author': 'habakuk', 'author_id': 254041, 'author_profile': 'https://Stackoverflow.com/users/254041', 'pm_score': 2, 'selected': False, 'text': "<p>Did you try this?</p>\n\n<pre><code>Dim settings = New XmlWriterSettings With {.Indent = True,\n                                          .NamespaceHandling = NamespaceHandling.OmitDuplicates,\n                                          .OmitXmlDeclaration = True}\nDim s As New MemoryStream\nUsing writer = XmlWriter.Create(s, settings)\n    ...\nEnd Using\n</code></pre>\n\n<p>Interesting is the 'NamespaceHandling.OmitDuplicates'</p>\n"}]
['https://Stackoverflow.com/questions/26743', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2892/']