title: Insert node in xml document using c#
body: <p>I was trying to insert an XML node in XML document at a specific position.</p>
  
  <p>This is my xml:</p>
  
  <pre><code>&lt;Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;Body&gt;
          &lt;readContract xmlns="http://implementation.company.schema.reference"&gt;
              &lt;ContactNumbers&gt;10158&lt;/ContactNumbers&gt;
              &lt;productGroups&gt;0085&lt;/productGroups&gt;
              &lt;indicationBalanceInfo&gt;false&lt;/indicationBalanceInfo&gt;
              &lt;indicationBlocked&gt;true&lt;/indicationBlocked&gt;
          &lt;/readContract&gt;
      &lt;/Body&gt;
  &lt;/Envelope&gt;
  </code></pre>
  
  <p>And am trying to insert another tag <code>&lt;productGroups&gt;0093&lt;/productGroups&gt;</code> below to the tag <code>&lt;productGroups&gt;0085&lt;/productGroups&gt;</code></p>
  
  <p>Expecting like the below:</p>
  
  <pre><code>&lt;Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;Body&gt;
          &lt;readContract xmlns="http://implementation.company.schema.reference"&gt;
              &lt;ContactNumbers&gt;10158&lt;/ContactNumbers&gt;
              &lt;productGroups&gt;0085&lt;/productGroups&gt;
              &lt;productGroups&gt;0093&lt;/productGroups&gt;
              &lt;indicationBalanceInfo&gt;false&lt;/indicationBalanceInfo&gt;
              &lt;indicationBlocked&gt;true&lt;/indicationBlocked&gt;
          &lt;/readContract&gt;
      &lt;/Body&gt;
  &lt;/Envelope&gt;
  </code></pre>
  
  <p>Used the below C# code to achieve it.</p>
  
  <pre><code>XmlDocument doc = new XmlDocument();
  string inputxml = this.StServiceCallActivity5.InputEnvelope.InnerXml.ToString();
  //Here inputxml contains whole xml document.
  string addxml = "&lt;productGroups&gt;0093&lt;/productGroups&gt;";
  doc.LoadXml(inputxml);
  XmlDocumentFragment xmlDocFrag = doc.CreateDocumentFragment();
  xmlDocFrag.InnerXml = addxml;
  XmlElement parentEle = doc.DocumentElement;
  parentEle.AppendChild(xmlDocFrag);
  </code></pre>
  
  <p>And it returns value like </p>
  
  <pre><code>&lt;Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;Body&gt;
          &lt;readContract xmlns="http://implementation.company.schema.reference"&gt;
              &lt;ContactNumbers&gt;10158&lt;/ContactNumbers&gt;
              &lt;productGroups&gt;0085&lt;/productGroups&gt;
              &lt;productGroups&gt;0093&lt;/productGroups&gt;
              &lt;indicationBalanceInfo&gt;false&lt;/indicationBalanceInfo&gt;
              &lt;indicationBlocked&gt;true&lt;/indicationBlocked&gt;
          &lt;/readContract&gt;
      &lt;/Body&gt;
      &lt;productGroups xmlns=""&gt;0093&lt;/productGroups&gt;
  &lt;/Envelope&gt;
  </code></pre>
  
  <p>Am a newbie to C# code, kindly help me to get the XML doc as expected.
  your help is much appreciated.</p>