title: Renaming Xml node
body: <p>I try to rename any node starts with "NP" to "NP" in an <code>XmlDocument</code>. I only found <a href="https://stackoverflow.com/questions/475293/change-the-node-names-in-an-xml-file-using-c-sharp">this related questoin</a>. based on the most up-voted answer I wrote:</p>
  
  <pre><code> XmlNodeList npNodeList = PSbank.SelectNodes("//*[starts-with(name(), 'NP')]");
  
  foreach (XmlNode npNode in npNodeList)
  {
  
      // create new (renamed) Content node
      XmlNode newNode = PSbank.CreateElement("NP");
  
      // [if needed] copy existing Content children
      newNode.InnerXml = npNode.InnerXml;
  
      // replace existing Content node with newly renamed Content node
      npNode.ParentNode.InsertBefore(newNode, npNode);
      npNode.ParentNode.RemoveChild(npNode);
  }
  </code></pre>
  
  <p>However, it seems it doesn't work in my case. When an <code>NPX</code> node contains other <code>NPX</code> nodes, the inner nodes are not replaced because the code manipulates <code>XmlDocument</code> and replaces the outer node with a new created node that contains the old <code>innerXml</code>.  </p>
  
  <p>Example Xml:</p>
  
  <pre><code>   &lt;S&gt;
       &lt;NPC&gt;
          &lt;NP&gt; A &lt;/NP&gt;
          &lt;NPA&gt; B &lt;/NPA&gt;
       &lt;/NPC&gt;
       &lt;NPD&gt;
           C
       &lt;/NPD&gt;
    &lt;/S&gt;
  </code></pre>
  
  <p>I look for a more general and elegant answer for such a problem.</p>