【问题标题】:Add parent node by keeping the existing child node in existing xml file using Powershell-通过使用 Powershell 将现有子节点保留在现有 xml 文件中来添加父节点-
【发布时间】:2020-04-01 07:09:38
【问题描述】:

现有文件 我有以下 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 powershell


    【解决方案1】:

    更新:输入的 XML 文件没有正确组织员工;它们都在 Office 下“混合在一起”,尽管它们在我们看来是分组的,但没有指定哪些值应该组合在一起。

    这是一个将您的第一个文件转换为第二个文件的脚本,假设第一个文件中的员工元素总是以相同的顺序出现并且总是组合在一起。

    [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
    

    【讨论】:

    • 您共享的输出文件是我想要的,但是该节点不是我文件中的第一个子节点。
    • 我已经编辑了输出文件。你能帮忙吗?在新的输出文件中 不是第一个孩子
    • 我发布了另一个类似的 XML 文件更正查询。我相信你可以在那里帮助我。问题在stackoverflow.com/q/61096126/13179395
    • Manish,我会尽快查看其他查询。请把上面的那一项标记为已回答好吗?
    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多