现有文件我有以下xml文件。我想使用PowerShell添加缺少的节点。我需要帮助来生成新的XML文件。需要帮助。
<Office>
<EmpID>0001</EmpID>
<EmpName>John</EmpName>
<Dept>HR</Dept>
<Location>Ohio</Location>
<Designation>Associate</Designation>
<EmpID>0002</EmpID>
<EmpName>George</EmpName>
<Dept>IT</Dept>
<Location>California</Location>
<Designation>IT Engineer</Designation>
<EmpID>0003</EmpID>
<EmpName>James</EmpName>
<Dept>Fin</Dept>
<Location>NewYork</Location>
<Designation>Auditor</Designation>
</Office>
新的预期文件
<Office>
<OfficeName>NEWOFFICE</OfficeName>
<OfficeRegNuM>1234</OfficeRegNuM>
<OfficeAddress>Building 001</OfficeAddress>
<Employee> **#this is missing in existing file,How to add this Node
<EmpID>0001</EmpID>
<EmpName>John</EmpName>
<Dept>HR</Dept>
<Location>Ohio</Location>
<Designation>Associate</Designation>
</Employee>
<Employee>
<EmpID>0002</EmpID>
<EmpName>George</EmpName>
<Dept>IT</Dept>
<Location>California</Location>
<Designation>IT Engineer</Designation>
</Employee>
<Employee
<EmpID>0003</EmpID>
<EmpName>James</EmpName>
<Dept>Fin</Dept>
<Location>NewYork</Location>
<Designation>Auditor</Designation>
</Employee>
</Office>
我需要使用POwerShell生成上面的输出
更新:输入的XML文件不能正确地组织员工。它们在Office下都是“混合在一起”的,尽管在我们看来它们似乎是分组在一起的,但是没有什么可以指定应将哪些值分组在一起的。
这是一个脚本,该脚本基于第一个文件下的employee元素始终以相同顺序出现并且始终分组在一起的假设,将您的第一个文件转换为第二个文件。
[xml]$curXml = Get-Content test.xml
$empNode = $curXml.FirstChild.CloneNode($true)
$curXml.FirstChild.RemoveAll()
$newNode = $curXml.CreateElement("OfficeName")
$newNode.InnerText = "NEWOFFICE"
$n = $curXml.FirstChild.AppendChild($newNode)
$newNode = $curXml.CreateElement("OfficeRegNuM")
$newNode.InnerText = "1234"
$n = $curXml.FirstChild.AppendChild($newNode)
$newNode = $curXml.CreateElement("OfficeAddress")
$newNode.InnerText = "Building 001"
$n = $curXml.FirstChild.AppendChild($newNode)
foreach ($child in $empNode.GetEnumerator())
{
if ($child.Name -eq "EmpID") {
$newEmp = $curXml.CreateElement("Employee")
$n = $curXml.FirstChild.AppendChild($newEmp)
}
$n = $newEmp.AppendChild($child.Clone())
}
Write-Host $curXml.InnerXml
Set-Content -Value $curXml.OuterXml -path testOutput.xml
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句