Selecting nodes

LINQ to XML uses PowerShell to query the content of XML files. This is achieved by combining the methods 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("$pwd       2;cars.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 caseinsensitive. The selection of the element by name is casesensitive.

Although it is not the preferred approach, XPath can still be used by calling the static method XPathSelectElements, 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
3.145.8.8