如何根据Property Name值重新排列XML,然后按以下顺序保存:Hostname,DNSDomain,Environment,HWSystemType,SystemType,HWSystemModel
感谢您的帮助。
<Objects>
<Object>
<Property Name="Hostname">myServer</Property>
<Property Name="SystemType">Physical</Property>
<Property Name="HWSystemModel">Windows Server</Property>
<Property Name="HWSystemType">Unitary HW-System</Property>
<Property Name="DNSDomain">Domain.net</Property>
<Property Name="Environment">Testing</Property>
</Object>
</Objects>
答案 0 :(得分:0)
好的 - 你去吧。只需运行此脚本(sortxml.ps1)并将输出重定向到您选择的文件,例如sortxml.ps1 > sorted_xml_file.xml
注意:Format-Xml
需要Powershell Community Extensions模块。
sortedxml.ps1:
# replace 'old.xml' with the path to your unsorted xmlfile
[xml]$xml = gc old.xml
[xml]$xmlnew = gc old.xml
$names = @('Hostname', 'DNSDomain', 'Environment', 'HWSystemType', 'SystemType', 'HWSystemModel' )
$nodes = $xmlnew.Objects.Object.Property
foreach($i in 0..($names.length - 1)) {
foreach($j in $xml.Objects.Object.Property) {
if($j.Name -eq $names[$i]) {
$nodes[$i].Name = $j.Name
$nodes[$i]."#text" = $j."#text"
break
}
}
}
Format-Xml -inputobject $xmlnew
答案 1 :(得分:-1)
按@Name
对XML进行排序(但请注意,字母顺序与您列出的不同),请尝试
Select-Xml -Path Your.xml -XPath //Property | Select-Object -ExpandProperty Node | Sort-Object -Property Name