title: Delete xmlns namespace from XML file
body: <p>I would like to remove the first <code>xmlns</code> attribute from a <code>XML</code> file because it's causing me some trouble in my <code>XSL</code> transformation.</p>
  <p>Here's the original xml :</p>
  <pre><code>&lt;ns4:ExportReferences xmlns=&quot;urn:be:fgov:ehealth:samws:v2:refdata&quot; xmlns:ns2=&quot;urn:be:fgov:ehealth:samws:v2:core&quot; xmlns:ns3=&quot;urn:be:fgov:ehealth:samws:v2:consultation&quot; xmlns:ns4=&quot;urn:be:fgov:ehealth:samws:v2:export&quot;&gt;
    &lt;AtcClassification code=&quot;A&quot;&gt;
      &lt;Description&gt;Alimentary Tract and Metabolism&lt;/Description&gt;
    &lt;/AtcClassification&gt;
    &lt;AtcClassification code=&quot;A01&quot;&gt;
      &lt;Description&gt;Stomatological Preparations&lt;/Description&gt;
    &lt;/AtcClassification&gt;
  &lt;/ns4:ExportReferences&gt;
  </code></pre>
  <p>I first tried with a <code>Regex</code> :
  <code>Regex.IsMatch(xml, @&quot;xmlns=(.*?)&quot;)</code> gives me a true but <code>Regex.Replace(xml, pattern, &quot;&quot;)</code> don't works.</p>
  <p>Then i tried to delete it with :</p>
  <pre><code>XDocument doc = XDocument.Load(@&quot;file.xml&quot;);
  foreach (var attr in doc.Descendants().Attributes()){
    if (attr.Name == &quot;xmlns&quot;)
      attr.Remove();}
  </code></pre>
  <p>And a lot of other tries but i always have the same behavior that i don't understand :
  I can delete the first <code>xmlns</code> attribute but when i save the file it appears now in every elements :</p>
  <pre><code>&lt;ns4:ExportReferences xmlns=&quot;urn:be:fgov:ehealth:samws:v2:refdata&quot; xmlns:ns2=&quot;urn:be:fgov:ehealth:samws:v2:core&quot; xmlns:ns3=&quot;urn:be:fgov:ehealth:samws:v2:consultation&quot; xmlns:ns4=&quot;urn:be:fgov:ehealth:samws:v2:export&quot;&gt;
    &lt;AtcClassification code=&quot;A&quot; xmlns=&quot;urn:be:fgov:ehealth:samws:v2:refdata&quot;&gt;
      &lt;Description&gt;Alimentary Tract and Metabolism&lt;/Description&gt;
    &lt;/AtcClassification&gt;
    &lt;AtcClassification code=&quot;A01&quot; xmlns=&quot;urn:be:fgov:ehealth:samws:v2:refdata&quot;&gt;
      &lt;Description&gt;Stomatological Preparations&lt;/Description&gt;
    &lt;/AtcClassification&gt;
  &lt;/ns4:ExportReferences&gt;
  </code></pre>
  <p>If i save it and make a second try with :</p>
  <pre><code>foreach (var attr in doc.Descendants().Attributes()){
    if (attr.NextAttribute != null &amp;&amp; attr.NextAttribute.Name == &quot;xmlns&quot;)
      attr.NextAttribute.Remove();}
  </code></pre>
  <p>It removes it as well but no changes made when reopening the file.</p>
  <p>Any helps would be really appreciated.</p>
  <p><strong>EDIT providing more infos</strong> :</p>
  <ol>
  <li>Input XML :</li>
  </ol>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  &lt;ns4:ExportReferences xmlns=&quot;urn:be:fgov:ehealth:samws:v2:refdata&quot; xmlns:ns2=&quot;urn:be:fgov:ehealth:samws:v2:core&quot; xmlns:ns3=&quot;urn:be:fgov:ehealth:samws:v2:consultation&quot; xmlns:ns4=&quot;urn:be:fgov:ehealth:samws:v2:export&quot; xmlns:ns5=&quot;urn:be:fgov:ehealth:samws:v2:actual:common&quot; xmlns:ns6=&quot;urn:be:fgov:ehealth:samws:v2:company:submit&quot; xmlns:ns7=&quot;urn:be:fgov:ehealth:samws:v2:reimbursement:submit&quot; xmlns:ns8=&quot;urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit&quot; xmlns:ns9=&quot;urn:be:fgov:ehealth:samws:v2:virtual:common&quot; xmlns:ns10=&quot;urn:be:fgov:ehealth:samws:v2:compounding:common&quot; xmlns:ns11=&quot;urn:be:fgov:ehealth:samws:v2:nonmedicinal:common&quot; xmlns:ns12=&quot;urn:be:fgov:ehealth:samws:v2:chapteriv:submit&quot; xmlns:ns13=&quot;urn:be:fgov:ehealth:samws:v2:actual:status&quot; version=&quot;5.0&quot; SamId=&quot;E.20201119_141847&quot;&gt;
      &lt;AtcClassification code=&quot;A&quot;&gt;
          &lt;Description&gt;Alimentary Tract and Metabolism&lt;/Description&gt;
      &lt;/AtcClassification&gt;
      &lt;AtcClassification code=&quot;A01&quot;&gt;
          &lt;Description&gt;Stomatological Preparations&lt;/Description&gt;
      &lt;/AtcClassification&gt;
      &lt;AtcClassification code=&quot;A01A&quot;&gt;
          &lt;Description&gt;Stomatological Preparations&lt;/Description&gt;
      &lt;/AtcClassification&gt;
      &lt;AtcClassification code=&quot;A01AA&quot;&gt;
          &lt;Description&gt;Caries Prophylactic Agents&lt;/Description&gt;
      &lt;/AtcClassification&gt;
      &lt;AtcClassification code=&quot;A01AA01&quot;&gt;
          &lt;Description&gt;Sodium Fluoride&lt;/Description&gt;
      &lt;/AtcClassification&gt;
      &lt;AtcClassification code=&quot;A01AA02&quot;&gt;
          &lt;Description&gt;Sodium Monofluorophosphate&lt;/Description&gt;
      &lt;/AtcClassification&gt;
  &lt;/ns4:ExportReferences&gt;
  </code></pre>
  <ol start="2">
  <li>XSLT :</li>
  </ol>
  <pre><code>&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  xmlns:ns2=&quot;urn:be:fgov:ehealth:samws:v2:core&quot;
  xmlns:ns3=&quot;urn:be:fgov:ehealth:samws:v2:consultation&quot;
  xmlns:ns4=&quot;urn:be:fgov:ehealth:samws:v2:export&quot;
  xmlns:ns5=&quot;urn:be:fgov:ehealth:samws:v2:actual:common&quot;
  xmlns:ns6=&quot;urn:be:fgov:ehealth:samws:v2:company:submit&quot;
  xmlns:ns7=&quot;urn:be:fgov:ehealth:samws:v2:reimbursement:submit&quot;
  xmlns:ns8=&quot;urn:be:fgov:ehealth:samws:v2:reimbursementlaw:submit&quot;
  xmlns:ns9=&quot;urn:be:fgov:ehealth:samws:v2:virtual:common&quot;
  xmlns:ns10=&quot;urn:be:fgov:ehealth:samws:v2:compounding:common&quot;
  xmlns:ns11=&quot;urn:be:fgov:ehealth:samws:v2:nonmedicinal:common&quot;
  xmlns:ns12=&quot;urn:be:fgov:ehealth:samws:v2:chapteriv:submit&quot;
  xmlns:ns13=&quot;urn:be:fgov:ehealth:samws:v2:actual:status&quot;&gt;
  &lt;xsl:template match=&quot;/ns4:ExportReferences&quot;&gt;
    &lt;xsl:for-each select=&quot;AtcClassification&quot;&gt;
      &lt;xsl:text&gt;INSERT INTO ATC VALUES ('&lt;/xsl:text&gt;
        &lt;xsl:value-of select=&quot;@code&quot; /&gt;
        &lt;xsl:text&gt;','&lt;/xsl:text&gt;
          &lt;xsl:value-of select=&quot;Description&quot; /&gt;
        &lt;xsl:text&gt;');&lt;/xsl:text&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <ol start="3">
  <li>Desired output :</li>
  </ol>
  <pre><code>INSERT INTO ATC VALUES ('A','Alimentary Tract and Metabolism');INSERT INTO ATC VALUES ('A01','Stomatological Preparations');INSERT INTO ATC VALUES ('A01A','Stomatological Preparations');INSERT INTO ATC VALUES ('A01AA','Caries Prophylactic Agents');INSERT INTO ATC VALUES ('A01AA01','Sodium Fluoride');INSERT INTO ATC VALUES ('A01AA02','Sodium Monofluorophosphate');
  </code></pre>
  <ol start="4">
  <li>XSLT processor and its version :</li>
  </ol>
  <p><code>xsl:vendor</code> = Microsoft
  <code>xsl:version</code> = 1</p>