title: How to create a new System.Xml.Linq.XElement with PowerShell
body: <p>I want to create an XML DOM programmatically using the <strong>System.Xml.Linq</strong> objects. I'd rather <strong>not</strong> parse a string or load a file from disk to create the DOM. In C# this is easy enough, but trying to do this in PowerShell does not seem possible.</p>
  
  <p><strong>Option 1</strong>: Doesn't work</p>
  
  <pre><code>$xlinq = [Reflection.Assembly]::Load("System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
  $el = new-Object System.Xml.Linq.XElement "foo"
  </code></pre>
  
  <p>This gives the following error:</p>
  
  <pre><code>new-Object : Cannot convert argument "0", with value: "foo",
   for "XElement" to type "System.Xml.Linq.XElement": "Cannot convert value "foo" to
   type "System.Xml.Linq.XElement". Error: "Data at the root level is invalid. 
   Line 1, position 1.""
  </code></pre>
  
  <p><strong>Option 2:</strong> Doesn't work</p>
  
  <pre><code>$xlinq = [Reflection.Assembly]::Load("System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
  $xname = New-Object System.Xml.Linq.XName "foo"
  $el = new-Object System.Xml.Linq.XElement $xname
  </code></pre>
  
  <p>It gives this error:</p>
  
  <pre><code>New-Object : Constructor not found. Cannot find an appropriate constructor for type System.Xml.Linq.XName.
  </code></pre>
  
  <p>According to MSDN (<a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx</a>) "XName does not contain any public constructors. Instead, this class provides an implicit conversion from String that allows you to create an XName." </p>