问题
I have an XML file of the following format:
<xml>
<data>
<foo float="99.0"/>
<bar float="12.0"/>
<tribble bool="true"/>
...
<flibble int="1"/>
</data>
</xml>
If I fetch that data in Powershell, I can see all the child elements of data with a Get-Memeber:
> $xmlData = [xml](Get-Content myfile.xml)
> $xmlData.data | Get-Member
...
foo Property System.Xml.XmlElement ...
bar Property System.Xml.XmlElement ...
tribble Property System.Xml.XmlElement ...
...
And I can access each child element individually. But how would I iterate over all the children, processing them with pipes? I'd love to be able to write something like this:
> $xmlData.data.Children | ?{$_ -eq "foo"}
...But alas that's just wishful thinking.
EDIT: Ok so I can reflect on the properties like this:
> $xmlData.data | get-member -memberType Property | ?{$_.Name -eq "foo"}
But I can't then go from the property (above I'm operating on a MemberDefinition) to the actual child element. Or can I?
回答1:
First: You need to use Get-Member -Force
... which will show you everything. Note with particular interest the get_* methods which represent properties which for some reason aren't exposed as properties in PowerShell. So when you see get_ChildNodes
in the member list, it means there's a .ChildNodes property on the object (even though PowerShell hides it for some reason).
Second: So therefore, there are lots of ways, including the get-member method you hinted at (I append the Format-Table
because it makes it easier to tell that you got something):
$xmlData.data.SelectNodes("*") | ft name, OuterXml
$xmlData.data.ChildNodes | ft name, OuterXml
$xmlData.data | gm -type Property | % { $xmlData.data.($_.Name) } |
Where { $_ -is [Xml.XmlElement] } | ft Name, OuterXml
回答2:
For some reason Get-Member
doesn't display the other properties System.Xml.XmlElement
provides. Here is the full member list: http://msdn.microsoft.com/en-us/library/system.xml.xmlelement_members.aspx
As you can see, there is a ChildNodes
property, so you can do this:
(Select-Xml -Path "file.xml" -XPath "//xml/data").Node.ChildNodes | Where-Object { $_.Name -eq "foo" }
来源:https://stackoverflow.com/questions/3389541/how-to-iterate-the-irregularly-named-children-of-an-xml-xmlelement-in-powershell