Selecting nodes

LINQ to XML uses PowerShell to query the content of XML files. This is achieved by combining the methods that are made available through an XDocument (or XContainer or XElement). Methods are available to find attributes and elements, either as immediate children or deeper within a document:

$xDocument = [System.Xml.Linq.XDocument]::Load("$pwdcars.xml") 
$xDocument.Descendants('car').
    Where( { $_.Element('colour').Value -eq 'Green' } ).
    Element('engine') 

The XML-specific methods are supplemented by .Linq extension methods, such as the Where method, to filter content.

As the query a script block encapsulated by the Where method—is native PowerShell, the comparison operation (-eq) is case insensitive. The selection of the element by name is case-sensitive.

Although it is not the preferred approach, XPath can still be used by calling the XPathSelectElements static method, as shown here:

[System.Xml.XPath.Extensions]::XPathSelectElements( 
    $xDocument, 
    '//car[colour="Green"]/engine' 
) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.61.16