简体   繁体   English

如何使用PowerShell附加到XML文件?

[英]How to append to XML file using PowerShell?

I have two XML files. 我有两个XML文件。 I want to append a node from the 1st XML to 2nd XML. 我想将节点从第一XML附加到第二XML。

This my 1st XML: 这是我的第一个XML:

<?xml version="1.0" encoding="UTF-8"?>
<UUT>
    <Auto>
        <Process>AutoGM</Process>
    </Auto>

    <UnitInfo>
        <SN>5CD1234567</SN>
        <MAC>1062E5961370</MAC>
        <SSID>1062E5961370</SSID>
    </UnitInfo> 
</UUT>

I want to append this node to my 2nd xml. 我想将此节点附加到第二个xml中。

<UnitInfo>
    <SN>5CD1234567</SN>
    <MAC>1062E5961370</MAC>
    <SSID>1062E5961370</SSID>
</UnitInfo>

This is my second XML: 这是我的第二个XML:

<?xml version="1.0" encoding="UTF-8"?>
<PC>
    <Platform>
        <SSID1>8549</SSID1><SSID2>5678</SSID2>
    </Platform>                
</PC>

The 2nd XML should look like this: 第二个XML应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<PC>
    <Platform>
        <SSID1>8549</SSID1><SSID2>5678</SSID2>
    </Platform> 

    <UnitInfo>
    <SN>5CD1234567</SN>
    <MAC>1062E5961370</MAC>
    <SSID>1062E5961370</SSID>
    </UnitInfo>

</PC>

I tried this 我试过了

Updated 更新

[xml]$Read_JOB = Get-Content "D:\Process\first.xml" 
[xml]$Read_UUT = Get-Content "D:\Process\second.xml"

$Read_JOB.JOB.AppendChild($Read_JOB.ImportNode(($Read_UUT.UUT.UUTInfo), $true))
$Read_JOB.Save("D:\Process\second.xml")

But the output is like this: 但是输出是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<PC>
    <Platform>
        <SSID1>8549</SSID1><SSID2>5678</SSID2>
    </Platform>   
<UUT>
    <Auto>
        <Process>AutoGM</Process>
    </Auto>

    <UnitInfo>
        <SN>5CD1234567</SN>
        <MAC>1062E5961370</MAC>
        <SSID>1062E5961370</SSID>
    </UnitInfo> 
</UUT>

</PC>

This is work, I updated 这是工作,我更新了

If I got your point correctly, you want to load 1.xml, 2.xml and append them in 3.xml. 如果我的观点正确,您想加载1.xml,2.xml并将它们附加到3.xml中。

Something to start with: 开始于:

$output = 'path\output.xml'
# Document creation
[xml]$Final = New-Object System.Xml.Finalument
$Final.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><PC></PC>")

# Creation of the first node
$FE1 = $Final.CreateElement("Platform")

# Creation of a sub node LOOP through other file
$SE1 = $Final.CreateElement("SSID1")
$SET1 = $Final.CreateTextNode("8541")
$SE1.AppendChild($SET1)
$FE1.AppendChild($SE1)

$SE1 = $Final.CreateElement("SSID2")
$SET1 = $Final.CreateTextNode("8549")
$SE1.AppendChild($SET1)
$FE1.AppendChild($SE1)

# Add the node to the document
$Final.LastChild.AppendChild($FE1);

# Store to a file 
$Final.Save("$output")

<#
FE1 = First Element
SE1 = Second Element
SET1 = Text for "Second Element 
#>

You Just need to call the variables in a loop for each element. 您只需要为每个元素循环调用变量。

The following works for me using your example XML data. 以下使用您的示例XML数据为我工作。

$XML1 = [xml](Get-Content D:\Process\first.xml)
$NodeToAppend = $XML1.SelectSingleNode('//UnitInfo')
$XMLToUpdate = [xml](Get-Content D:\Process\second.xml)
$XMLToUpdate.PC.AppendChild($XMLToUpdate.ImportNode($NodeToAppend,$true))
$XMLToUpdate.Save("D:\Process\third.xml") # This can be second.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM