title: Removing an XML Nodes namespace prefix from a XML string with Regex
body: <p>I have some legacy XML documents stored in a database as a blob, which are not well formed XML. I'm reading them in from a SQL database, and ultimately, as I am using C#.NET, would like to instantiate them as an XMLDocument.</p>
  
  <p>When I try to do this, I obviously get an XMLException. Having looked at the XML documents, they are all failing because of undeclared namespaces in specific XML Nodes.</p>
  
  <p>I am not concerned with any of the XML nodes which have this prefix, so I can ignore them or throw them away. So basically, before I load the string as an XMLDocument, I would like to remove the prefix in the string, so that</p>
  
  <pre><code>&lt;tem:GetRouteID&gt;
          &lt;tem:PostCode&gt;postcode&lt;/tem:PostCode&gt;
          &lt;tem:Type&gt;ItemType&lt;/tem:Type&gt;
  &lt;/tem:GetRouteID&gt;
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>&lt;GetRouteID&gt;
      &lt;PostCode&gt;postcode&lt;/PostCode&gt;
      &lt;Type&gt;ItemType&lt;/Type&gt;
  &lt;/GetRouteID&gt;
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>&lt;wsse:Security soapenv:actor=""&gt;
      &lt;wsse:BinarySecurityToken&gt;token&lt;/wsse:BinarySecurityToken&gt;
  &lt;/wsse:Security&gt;
  </code></pre>
  
  <p>becomes this :</p>
  
  <pre><code>&lt;Security soapenv:actor=""&gt;
      &lt;BinarySecurityToken&gt;token&lt;/BinarySecurityToken&gt;
  &lt;/Security&gt;
  </code></pre>
  
  <p>I have one solution which does this like so :</p>
  
  <pre><code>&lt;appSettings&gt;
    &lt;add key="STRIP_NAMESPACES" value="wsse;tem" /&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  
  
  <pre><code>if (STRIP_NAMESPACES != null)
  {
      string[] namespaces = Regex.Split(STRIP_NAMESPACES, ";");
  
      foreach (string ns in namespaces)
     {
          str2 = str2.Replace("&lt;" + ns + ":", "&lt;"); // Replace opening tag
          str2 = str2.Replace("&lt;/" + ns + ":", "&lt;/"); // Replace closing tag
  
      }
  }
  </code></pre>
  
  <p>but Ideally I would like a generic approach for this, so I don't have to endlessly configure the namespaces I want to remove.</p>
  
  <p>How can I achieve this in C#.NET. I am assuming that a Regex is the way to go here?</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Ria's Regex below works well for the requirement above. However, how would I need to change the Regex to also change this</p>
  
  <pre><code>&lt;wsse:Security soapenv:actor=""&gt;
      &lt;BinarySecurityToken&gt;authtoken&lt;/BinarySecurityToken&gt;
  &lt;/Security&gt;
  </code></pre>
  
  <p>to this?</p>
  
  <pre><code>&lt;Security&gt;
      &lt;BinarySecurityToken&gt;authtoken&lt;/BinarySecurityToken&gt;
  &lt;/Security&gt;
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Think I've worked out the updated version myself based on Ria's answer like so :</p>
  
  <pre><code>&lt;(/?)\w+:(\w+/?) ?(\w+:\w+.*)?&gt;
  </code></pre>