title: Exception occured while tried to use 'InsertBefore' of XmlDocument in C#
body: <p><br>
           I was trying to insert a xml node before another xmlnode and I have got an exception saying "The reference node is not a child of this node."</p>
  
  <p>This is my initial xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;sampleData&gt;
      &lt;otherNodes&gt;&lt;/otherNodes&gt;
    &lt;/sampleData&gt;
  &lt;/Details&gt;
  </code></pre>
  
  <p>I wanted to insert following xml datas(b:dataTobeInserted1,b:dataTobeInserted2 and b:dataTobeInserted3) as a Child of <strong>Details</strong> but before <strong>sampleData</strong>.</p>
  
  <p>Details1.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;DataInserted1 xmlns:b="http://example.com/data"&gt;
    &lt;b:dataTobeInserted1&gt;
      &lt;b:otherDetails1&gt;&lt;/b:otherDetails1&gt;
    &lt;/b:dataTobeInserted1&gt;
  &lt;/DataInserted1&gt;
  </code></pre>
  
  <p>Details2.xml  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;DataInserted2 xmlns:b="http://example.com/data"&gt;
    &lt;b:dataTobeInserted2&gt;
      &lt;b:otherDetails2&gt;&lt;/b:otherDetails2&gt;
    &lt;/b:dataTobeInserted2&gt;
  &lt;/DataInserted2&gt;
  </code></pre>
  
  <p>Details3.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;DataInserted3 xmlns:b="http://example.com/data"&gt;
    &lt;b:dataTobeInserted3&gt;
      &lt;b:otherDetails3&gt;&lt;/b:otherDetails3&gt;
    &lt;/b:dataTobeInserted3&gt;
  &lt;/DataInserted3&gt;
  </code></pre>
  
  <p>I want my Output as </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:b="http://example.com/data"&gt;
    &lt;b:dataTobeInserted1&gt;
      &lt;b:otherDetails1&gt;&lt;/b:otherDetails1&gt;
    &lt;/b:dataTobeInserted1&gt;
    &lt;b:dataTobeInserted2&gt;
      &lt;b:otherDetails2&gt;&lt;/b:otherDetails2&gt;
    &lt;/b:dataTobeInserted2&gt;
    &lt;b:dataTobeInserted3&gt;
      &lt;b:otherDetails3&gt;&lt;/b:otherDetails3&gt;
    &lt;/b:dataTobeInserted3&gt;
    &lt;sampleData&gt;
      &lt;otherNodes&gt;&lt;/otherNodes&gt;
    &lt;/sampleData&gt;
  &lt;/Details&gt;
  </code></pre>
  
  <p>This is what I did to achieve my desired output.</p>
  
  <pre><code>    XmlDocument xmldoc = new XmlDocument();
      xmldoc.Load(@"..\..\initial-Doc.xml");
  
      xmldoc.DocumentElement.SetAttribute("xmlns:b", "http://example.com/data");
  
  
          XmlDocument detail1 = new XmlDocument();
          detail1.Load(@"..\..\DataToBeInserted1.xml");
          XmlNode detail1Node = xmldoc.ImportNode(detail1.DocumentElement, true);
  
          XmlDocument detail2 = new XmlDocument();
          detail2.Load(@"..\..\DataToBeInserted2.xml");
          XmlNode detail2Node = xmldoc.ImportNode(detail2.DocumentElement, true);
  
          XmlDocument detail3 = new XmlDocument();
          detail3.Load(@"..\..\DataToBeInserted3.xml");
          XmlNode detail3Node = xmldoc.ImportNode(detail3.DocumentElement, true);
  
      xmldoc.InsertBefore(detail1Node, xmldoc.DocumentElement.FirstChild);
      xmldoc.InsertBefore(detail2Node, xmldoc.DocumentElement.FirstChild);
      xmldoc.InsertBefore(detail3Node, xmldoc.DocumentElement.FirstChild);
  
      xmldoc.Save(@"..\..\initial-Doc-new.xml");
  </code></pre>
  
  <p>Is the new namespace is causing the problem?Please tell me where I went wrong.</p>
  
  <p>Thanks
  Alex</p>