title: Add XML after specific node
body: <p>I have the following XML document already loaded as an XElement (<code>$el</code>):</p>
  
  
  
  <pre><code>&lt;a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
      &lt;d&gt;&lt;/d&gt;
      &lt;e&gt;&lt;/e&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>How can I use PowerShell to append another XElement (<code>$theElement</code>) beneath <code>&lt;c&gt;&lt;/c&gt;</code>? I tried the following:</p>
  
  <pre><code>$where = {
      param ($item)
      return ($item.Name.LocalName -eq "c")
  }
  
  $el.Descendants().Where($where).FirstOrDefault().Add($theElement)
  </code></pre>
  
  <p>but this errors out:</p>
  
  <blockquote>
    <p>Method invocation failed because [System.Collections.ObjectModel.Collection`1[[System.Management.Automation.PSObject,System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not contain a method named 'FirstOrDefault'.</p>
  </blockquote>
  
  <p>Note: <code>$el</code> and <code>$theElement</code> has to be <code>System.Xml.Linq.XElement</code> objects.</p>