我正在尝试使用powershell加载xml并显示没有设置父级的节点的所有名称。这可能吗?
到目前为止,这是我的代码:
[xml]$changesXML = Get-Content $PathToXML
function ReplaceOneFormat
{
Param(
$Parent
)
foreach($child in $Parent)
{
$child.Name?
#ReplaceOneFormat $child # call this function by recursive
}
}
ReplaceOneFormat $changesXML
所以,这是我的例子xml:
<?xml version="1.0"?>
<books>
<book author="a">1</book>
<book author="b">2</book>
<book author="c">
<page>796</page>
<language>USA</language>
<Publisher>USAFM</Publisher>
</book>
<journal>L2</journal>
<journal>
<book author="d">L3</book>
<Publisher>USAFM</Publisher>
</journal>
</books>
我需要这样的结果:
Node: Value:
books
book 1
book 2
book
page 796
language USA
Publisher USAFM
journal L2
journal
book L3
Publisher USAFM
答案 0 :(得分:1)
我创建示例,循环所有节点。希望这对你有帮助!
[xml]$changesXML = Get-Content $NameFile
$tmp = $changesXML.SelectNodes("//*")
$cnt = $tmp.Count
for ($i = 0; $i -lt $tmp.Count; $i++) {
"Attributes:"+$tmp.Item($i).Attributes
"BaseURI:"+$tmp.Item($i).BaseURI
"ChildNodes:"+$tmp.Item($i).ChildNodes
"InnerText:"+$tmp.Item($i).InnerText
"Name:"+$tmp.Item($i).Name
"LocalName:"+$tmp.Item($i).LocalName
"NamespaceURI:"+$tmp.Item($i).NamespaceURI
"NodeType:"+$tmp.Item($i).NodeType
"Prefix:"+$tmp.Item($i).Prefix
"**********************************************"
}