title: Merge XML documents into empty XML node?
body: <p><strong>EDIT</strong>: This isn't quite the same as the posted that was linked. The main problem I was running into was appending child nodes to an empty XML node. When selecting the node directly, it would return a <code>System.String</code> type, which doesn't have the <code>AppendChild</code> method. The fix was to select all child nodes and then filter that by name, per Dan's suggestion below. </p>
  
  <pre class="lang-ps prettyprint-override"><code>$emptyNode= $root.ChildNodes | ? { $_.name -eq "customers" }
  </code></pre>
  
  <hr>
  
  <p>I'm mostly using powershell, but underneath a lot of the code I'm working with is using .NET System object. What I'm looking to do is probably better explained through an example. Say I have three XML documents:</p>
  
  <pre><code>&lt;!-- XML File A --&gt;
  &lt;customer&gt;
      &lt;name&gt;ACME Co&lt;/name&gt;
      &lt;users&gt;
          &lt;user&gt;
              &lt;name&gt;Alex&lt;/name&gt;
              &lt;age&gt;20&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Aaron&lt;/name&gt;
              &lt;age&gt;21&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Allison&lt;/name&gt;
              &lt;age&gt;22&lt;/age&gt;
          &lt;/user&gt;
      &lt;/users&gt;
  &lt;/customer&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;!-- XML File B --&gt;
  &lt;customer&gt;
      &lt;name&gt;Big Co&lt;/name&gt;
      &lt;users&gt;
          &lt;user&gt;
              &lt;name&gt;Bob&lt;/name&gt;
              &lt;age&gt;30&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Barry&lt;/name&gt;
              &lt;age&gt;31&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Bill&lt;/name&gt;
              &lt;age&gt;32&lt;/age&gt;
          &lt;/user&gt;
      &lt;/users&gt;
  &lt;/customer&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;!-- XML File C --&gt;
  &lt;customer&gt;
      &lt;name&gt;Cool Co&lt;/name&gt;
      &lt;users&gt;
          &lt;user&gt;
              &lt;name&gt;Carl&lt;/name&gt;
              &lt;age&gt;40&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Craig&lt;/name&gt;
              &lt;age&gt;41&lt;/age&gt;
          &lt;/user&gt;
          &lt;user&gt;
              &lt;name&gt;Chris&lt;/name&gt;
              &lt;age&gt;42&lt;/age&gt;
          &lt;/user&gt;
      &lt;/users&gt;
  &lt;/customer&gt;
  </code></pre>
  
  <p>I have a "root" document that looks like:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8' ?&gt;
  &lt;customers&gt;
  &lt;/customers&gt;
  </code></pre>
  
  <p>I want to combine the three A, B, and C documents under the root doc so my final product will be:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8' ?&gt;
  &lt;customers&gt;
      &lt;!-- XML File A --&gt;
      &lt;customer&gt;
          &lt;name&gt;ACME&lt;/name&gt;
          &lt;users&gt;
              &lt;user&gt;
                  &lt;name&gt;Alex&lt;/name&gt;
                  &lt;age&gt;20&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Aaron&lt;/name&gt;
                  &lt;age&gt;21&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Allison&lt;/name&gt;
                  &lt;age&gt;22&lt;/age&gt;
              &lt;/user&gt;
          &lt;/users&gt;
      &lt;/customer&gt;
  
      &lt;!-- XML File B --&gt;
      &lt;customer&gt;
          &lt;name&gt;Big Co&lt;/name&gt;
          &lt;users&gt;
              &lt;user&gt;
                  &lt;name&gt;Bob&lt;/name&gt;
                  &lt;age&gt;30&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Barry&lt;/name&gt;
                  &lt;age&gt;31&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Bill&lt;/name&gt;
                  &lt;age&gt;32&lt;/age&gt;
              &lt;/user&gt;
          &lt;/users&gt;
      &lt;/customer&gt;
  
      &lt;!-- XML File C --&gt;
      &lt;customer&gt;
          &lt;name&gt;Cool Co&lt;/name&gt;
          &lt;users&gt;
              &lt;user&gt;
                  &lt;name&gt;Carl&lt;/name&gt;
                  &lt;age&gt;40&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Craig&lt;/name&gt;
                  &lt;age&gt;41&lt;/age&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;name&gt;Chris&lt;/name&gt;
                  &lt;age&gt;42&lt;/age&gt;
              &lt;/user&gt;
          &lt;/users&gt;
      &lt;/customer&gt;
  &lt;/customers&gt;
  </code></pre>
  
  <p>I've been looking at AppendChild and ImportNode but I keep getting various errors. One thing, is that in my root document, the single empty node <code>customers</code> is listed a type of System.String rather than an XmlNode, so I can't append any children. See this Powershell snippet:</p>
  
  <pre class="lang-ps prettyprint-override"><code>$doc = New-Object System.Xml.XmlDocument
  $doc.LoadXml("&lt;?xml version='1.0' encoding='utf-8' ?&gt;&lt;customers&gt;&lt;/customers&gt;")
  $doc.customers.GetType()
  
  IsPublic IsSerial Name    BaseType
  -------- -------- ----    --------
  True     True     String  System.Object
  </code></pre>
  
  <p>That doesn't even matter that much though, because if I try to import the node, I get an error <code>Cannot import nodes of type 'Document'.</code>. </p>
  
  <pre class="lang-ps prettyprint-override"><code>$docA = New-Object System.Xml.XmlDocument
  $docA.LoadXml("&lt;customer&gt;&lt;name&gt;ACME&lt;/name&gt;&lt;users&gt;&lt;user&gt;&lt;name&gt;Alex&lt;/name&gt;&lt;age&gt;20&lt;/age&gt;&lt;/user&gt;&lt;user&gt;&lt;name&gt;Aaron&lt;/name&gt;&lt;age&gt;21&lt;/age&gt;&lt;/user&gt;&lt;user&gt;&lt;name&gt;Allison&lt;/name&gt;&lt;age&gt;22&lt;/age&gt;&lt;/user&gt;&lt;/users&gt;&lt;/customer&gt;")
  $docAImported = $doc.ImportNode($docA, $true)
  
  Exception calling "ImportNode" with "2" argument(s): "Cannot import nodes of type 'Document'."
  At line:1 char:32
  + $docAImported = $doc.ImportNode &lt;&lt;&lt;&lt; ($docA, $true)
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>Any help would be greatly appreciated. Thanks!</p>