2017-07-04 2 views
0

ich folgende XML haben:Wie Sie einen Wert aus XML-Datei (keine eindeutige ID für den Knoten)

<?xml version="1.0"?> 
<Objects> 
    <Object Type="System.Management.Automation.PSCustomObject"> 
     <Property Type="System.String" Name="FolderName">Mozilla_Firefox</Property> 
     <Property Type="System.Int32" Name="Export">6</Property> 
    </Object> 
    <Object Type="System.Management.Automation.PSCustomObject"> 
     <Property Type="" Name="ExportListing"/> 
     <Property Type="System.String" Name="FolderName">Notepad</Property> 
     <Property Type="System.Int32" Name="ExportCountZips">1</Property> 
    </Object> 
</Objects> 

Ich möchte insbesondere einen Wert von Immobilien ‚Export‘ bekommen ‚Ordnername‘. Hier ist, wie ich eine XML-Linie mit dem Editor erhalten:

$t = $xml.Objects.Object.Property | 
    ? { $_.Name -eq 'FolderName'} | 
    Where '#text' -eq 'Notepad' 

wie man einen richtigen ‚ExportCountZips‘ bekommen mit einem Wert von 1?

Antwort

1

Verwenden Sie ein XPath expression:

$name = 'Notepad' 
$xpath = "//Object[Property[@Name='FolderName']/text()='$name']/Property[@Name='ExportCountZips']" 
$xml.SelectSingleNode($xpath) | Select-Object -Expand '#text' 
1
$ExportCountZips = ($xml.objects.object.property | Where-Object {$_.Name -eq "ExportCountZips"})."#text" 
Verwandte Themen