title: C# XmlDocument Nodes
body: <p>I'm trying to access UPS tracking info and, as per their example, I need to build a request like so:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;AccessRequest xml:lang='en-US'&gt;
     &lt;AccessLicenseNumber&gt;YOURACCESSLICENSENUMBER&lt;/AccessLicenseNumber&gt;
     &lt;UserId&gt;YOURUSERID&lt;/UserId&gt;
     &lt;Password&gt;YOURPASSWORD&lt;/Password&gt;
  &lt;/AccessRequest&gt;
  &lt;?xml version="1.0" ?&gt;
  &lt;TrackRequest&gt;
     &lt;Request&gt;
       &lt;TransactionReference&gt;
           &lt;CustomerContext&gt;guidlikesubstance&lt;/CustomerContext&gt;
       &lt;/TransactionReference&gt;
       &lt;RequestAction&gt;Track&lt;/RequestAction&gt;
     &lt;/Request&gt;
     &lt;TrackingNumber&gt;1Z9999999999999999&lt;/TrackingNumber&gt;
  &lt;/TrackRequest&gt;
  </code></pre>
  
  <p>I'm having a problem creating this with 1 XmlDocument in C#. When I try to add the second:
  <code>&lt;?xml version="1.0" ?&gt; or the &lt;TrackRequest&gt;</code> 
  it throws an error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: This
    document already has a
    'DocumentElement' node.</p>
  </blockquote>
  
  <p>I'm guessing this is because a standard XmlDocument would only have 1 root node. Any ideas?</p>
  
  <p>Heres my code so far:</p>
  
  <pre><code>XmlDocument xmlDoc = new XmlDocument();
  XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
  XmlElement rootNode = xmlDoc.CreateElement("AccessRequest");
  rootNode.SetAttribute("xml:lang", "en-US");
  xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
  xmlDoc.AppendChild(rootNode);
  
  XmlElement licenseNode = xmlDoc.CreateElement("AccessLicenseNumber");
  XmlElement userIDNode = xmlDoc.CreateElement("UserId");
  XmlElement passwordNode = xmlDoc.CreateElement("Password");
  
  XmlText licenseText = xmlDoc.CreateTextNode("mylicense");
  XmlText userIDText = xmlDoc.CreateTextNode("myusername");
  XmlText passwordText = xmlDoc.CreateTextNode("mypassword");
  
  rootNode.AppendChild(licenseNode);
  rootNode.AppendChild(userIDNode);
  rootNode.AppendChild(passwordNode);
  
  licenseNode.AppendChild(licenseText);
  userIDNode.AppendChild(userIDText);
  passwordNode.AppendChild(passwordText);
  
  XmlElement rootNode2 = xmlDoc.CreateElement("TrackRequest");
  xmlDoc.AppendChild(rootNode2);
  </code></pre>