使用PowerShell将单个记录XML合并为多记录XML

时间:2015-04-23 15:28:39

标签: xml powershell

我需要使用PowerShell将多个单个记录XML文件合并为一个包含多个记录的XML。我是PowerShell的新手,但在合并XML时找到了一些不错的文档,但我无法让它们工作。我需要帮助弄清楚我做错了什么。

我已粘贴下面的PowerShell脚本,它运行时没有错误,但仅输出第一个XML文档中的数据(XMLRecord01.xml)源文件和预期输出的示例位于帖子的末尾。

POWERSHELL SCRIPT

******  XML 1  ******
<?xml version="1.0" encoding="UTF-8"?>
<TESTDataSet> 
  <Header>
    <HD01_01>HeaderData</HD01_01>
    <HD01_03>HeaderData</HD01_03>
    <HD01_04>HeaderData</HD01_04>
    <HD01_07>HeaderData</HD01_07>
    <HD01_08>HeaderData</HD01_08>
     <Record>
      <RD01>
        <RD01_01>UniqueRecordID.001</RD01_01>
        <RD01_02>Record001DATA</RD01_02>
        <RD01_03>Record001DATA</RD01_03>
        <RD01_04>Record001DATA</RD01_04>
      </RD01>
      <RD02>
        <RD02_01>MoreData4RecordID.001</RD02_01>
        <RD02_02>Record001DATA</RD02_02>
        <RD02_03>Record001DATA</RD02_03>
        <RD02_04>Record001DATA</RD02_04>
        <RD02_05>Record001DATA</RD02_05>
      </RD02>
     </Record>
  </Header>
</TESTDataSet>

****** XML2  ******
<?xml version="1.0" encoding="UTF-8"?>
<TESTDataSet> 
  <Header>
    <HD01_01>HeaderData</HD01_01>
    <HD01_03>HeaderData</HD01_03>
    <HD01_04>HeaderData</HD01_04>
    <HD01_07>HeaderData</HD01_07>
    <HD01_08>HeaderData</HD01_08>
     <Record>
      <RD01>
        <RD01_01>UniqueRecordID.002</RD01_01>
        <RD01_02>Record002DATA</RD01_02>
        <RD01_03>Record002DATA</RD01_03>
        <RD01_04>Record002DATA</RD01_04>
      </RD01>
      <RD02>
        <RD02_01>MoreData4RecordID.002</RD02_01>
        <RD02_02>Record002DATA</RD02_02>
        <RD02_03>Record002DATA</RD02_03>
        <RD02_04>Record002DATA</RD02_04>
        <RD02_05>Record002DATA</RD02_05>
      </RD02>
    </Record>
  </Header>
</TESTDataSet>


****** EXPECTED COMBINED XML ******
<?xml version="1.0" encoding="UTF-8"?>
<TESTDataSet> 
  <Header>
    <HD01_01>HeaderData</HD01_01>
    <HD01_03>HeaderData</HD01_03>
    <HD01_04>HeaderData</HD01_04>
    <HD01_07>HeaderData</HD01_07>
    <HD01_08>HeaderData</HD01_08>
     <Record>
      <RD01>
        <RD01_01>UniqueRecordID.001</RD01_01>
        <RD01_02>Record001DATA</RD01_02>
        <RD01_03>Record001DATA</RD01_03>
        <RD01_04>Record001DATA</RD01_04>
      </RD01>
      <RD02>
        <RD02_01>MoreData4RecordID.001</RD02_01>
        <RD02_02>Record001DATA</RD02_02>
        <RD02_03>Record001DATA</RD02_03>
        <RD02_04>Record001DATA</RD02_04>
        <RD02_05>Record001DATA</RD02_05>
      </RD02>
    </Record>
    <Record>
      <RD01>
        <RD01_01>UniqueRecordID.002</RD01_01>
        <RD01_02>Record002DATA</RD01_02>
        <RD01_03>Record002DATA</RD01_03>
        <RD01_04>Record002DATA</RD01_04>
      </RD01>
      <RD02>
        <RD02_01>MoreData4RecordID.002</RD02_01>
        <RD02_02>Record002DATA</RD02_02>
        <RD02_03>Record002DATA</RD02_03>
        <RD02_04>Record002DATA</RD02_04>
        <RD02_05>Record002DATA</RD02_05>
      </RD02>
    </Record>
  </Header>
</TESTDataSet>

示例输入/输出文件

http://localhost:8085

1 个答案:

答案 0 :(得分:0)

循环通过各个节点似乎有点过分。

为什么不连接这两个文件,将它们作为单个XML对象引入,然后removeChild(&#39;&#39;)?