System.Xml.Linq
The System.Xml.Linq
namespace was added with .NET 3.5. This is known as LINQ to XML. Language Integrated Query (LINQ) is used to describe a query in the same language as the rest of a program. Therefore, interacting with a complex XML document does not require the use of XPath
queries.
PowerShell can make use of System.Xml.Linq
once the required assembly has been added:
Add-Type -AssemblyName System.Xml.Linq
This can also be phrased as:
using assembly System.Xml.Linq
As a newer interface, System.Xml.Linq
tends to be more consistent. The same syntax is used to create a document from scratch that is used to add elements and so on.
Opening documents
The XDocument
class is used to load or parse a document. XML content may be cast to an XDocument
in the same way that content is cast using the [Xml]
type accelerator:
using assembly System.Xml.Linq
[System.Xml.Linq.XDocument]$xDocument = @"
<?xml version="1.0"?>
<cars>
<car type="Saloon">
<colour>Green<...