I have an XML file of the following format:
我有一个XML文件的格式如下:
<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:
如果我在Powershell中获取这些数据,我就可以通过一个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?
但是我不能从属性(上面是对MemberDefinition的操作)转到实际的子元素。或者我可以吗?
2 个解决方案
#1
6
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).
首先:你需要使用动员部队……它会告诉你一切。特别要注意的是get_*方法,它表示由于某些原因而不作为PowerShell中的属性公开的属性。因此,当您在成员列表中看到get_ChildNodes时,它意味着对象上有一个. childnodes属性(尽管PowerShell出于某种原因隐藏了它)。
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):
第二:因此,有很多方法,包括你提到的get-member方法(我添加了Format-Table,因为它可以更容易地告诉你得到了什么):
$xmlData.data.SelectNodes("*") | ft name, OuterXml
- $xmlData.data.SelectNodes("*") | ft name, OuterXml
$xmlData.data.ChildNodes | ft name, OuterXml
- xmlData.data美元。ChildNodes | ft name, OuterXml
$xmlData.data | gm -type Property | % { $xmlData.data.($_.Name) } |
Where { $_ -is [Xml.XmlElement] } | ft Name, OuterXml- xmlData美元。数据| gm -type属性| % {$xmlData.data.($_.Name)} |,其中{$_是[Xml]。} | ft Name, OuterXml
#2
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
出于某种原因,Get-Member不显示其他属性System.Xml。XmlElement提供。以下是完整的成员列表: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" }
(选择xml文件路径”。xml“xpath”/ / xml / .Node数据”)。ChildNodes | Where-Object{$_。名字eq“foo”}
#1
6
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).
首先:你需要使用动员部队……它会告诉你一切。特别要注意的是get_*方法,它表示由于某些原因而不作为PowerShell中的属性公开的属性。因此,当您在成员列表中看到get_ChildNodes时,它意味着对象上有一个. childnodes属性(尽管PowerShell出于某种原因隐藏了它)。
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):
第二:因此,有很多方法,包括你提到的get-member方法(我添加了Format-Table,因为它可以更容易地告诉你得到了什么):
$xmlData.data.SelectNodes("*") | ft name, OuterXml
- $xmlData.data.SelectNodes("*") | ft name, OuterXml
$xmlData.data.ChildNodes | ft name, OuterXml
- xmlData.data美元。ChildNodes | ft name, OuterXml
$xmlData.data | gm -type Property | % { $xmlData.data.($_.Name) } |
Where { $_ -is [Xml.XmlElement] } | ft Name, OuterXml- xmlData美元。数据| gm -type属性| % {$xmlData.data.($_.Name)} |,其中{$_是[Xml]。} | ft Name, OuterXml
#2
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
出于某种原因,Get-Member不显示其他属性System.Xml。XmlElement提供。以下是完整的成员列表: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" }
(选择xml文件路径”。xml“xpath”/ / xml / .Node数据”)。ChildNodes | Where-Object{$_。名字eq“foo”}