2014-04-01 54 views
2

我有一個XML文件中像下面得到一個XML文件的parant節點的直接子節點使用PowerShell

<ParentNode> 
    <childnode1> 
    <Name string="Code1"/> 
    </childnode1> 

    <childnode2> 
    <Name string="Code2"/> 
    </childnode2> 
    <childnode3> 
    <Name string="Code3"/> 
    </childnode3> 
</ParentNode> 

給我如何使用PowerShell

我試圖打印ParentNode的所有子節點通過使用給定的代碼

foreach($child in $xmlfile.ParentNode) 
{ 
Write-Host $child 
} 

它應該打印

childnode1 
childnode2 
childnode3 

回答

1
PS>[xml]$x=gc c:\temp\xml.xml  
PS>$x.ParentNode    

或者也許你想:

PS>$x.ParentNode |gm -MemberType property |select -expand name  
childnode1               
childnode2               
childnode3