title: C# CreateElement method - how to add an child element with xmlns=""
body: <p>How can I get the following code to add the element with "xmlns=''"? </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml; 
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string strXML = 
                  "&lt;myroot&gt;" + 
                  "   &lt;group3 xmlns='myGroup3SerializerStyle'&gt;" + 
                  "       &lt;firstname xmlns=''&gt;Neal3&lt;/firstname&gt;" + 
                  "   &lt;/group3&gt;" + 
                  "&lt;/myroot&gt;";
  
              XmlDocument xmlDoc = new XmlDocument();
              xmlDoc.LoadXml(strXML);
  
              XmlElement elem = xmlDoc.CreateElement(null, "lastname", null);
              elem.InnerText = "New-Value";
  
              string strXPath = "/myroot/*[local-name()='group3' and namespace-uri()='myGroup3SerializerStyle']/firstname";
              XmlNode insertPoint = xmlDoc.SelectSingleNode(strXPath);
              insertPoint.AppendChild(elem);
  
              string resultOuter = xmlDoc.OuterXml;
  
              Console.WriteLine("\n resultOuter=" + resultOuter);
  
              Console.ReadLine(); 
  
          }
      }
  }
  </code></pre>
  
  <p>My current output: </p>
  
  <pre><code> resultOuter=&lt;myroot&gt;&lt;group3 xmlns="myGroup3SerializerStyle"&gt;&lt;firstname xmlns=""
  &gt;Neal3&lt;lastname&gt;New-Value&lt;/lastname&gt;&lt;/firstname&gt;&lt;/group3&gt;&lt;/myroot&gt;
  </code></pre>
  
  <p>The desired output: </p>
  
  <pre><code> resultOuter=&lt;myroot&gt;&lt;group3 xmlns="myGroup3SerializerStyle"&gt;&lt;firstname xmlns=""
  &gt;Neal3&lt;lastname xmlns=""&gt;New-Value&lt;/lastname&gt;&lt;/firstname&gt;&lt;/group3&gt;&lt;/myroot&gt;
  </code></pre>
  
  <p>For background, see related posts: 
  <a href="http://www.stylusstudio.com/ssdn/default.asp?fid=23" rel="nofollow noreferrer">http://www.stylusstudio.com/ssdn/default.asp?fid=23</a> (today) </p>
  
  <p><a href="https://stackoverflow.com/questions/2410620/net-xmlserializer-to-element-formdefaultunqualified-xml">.NET XmlSerializer to Element FormDefault=Unqualified XML?</a> (March 9, thought I fixed it, but bit me again today!) </p>