将现有XML文件中的节点附加到新的XML文档中

时间:2018-06-18 18:58:54

标签: xml powershell

我目前正在处理我正在使用的某些XML文件的版本控制功能。基本的工作流程是,当解析XML文件时,它被Order拆分然后放入数据库表,然后根据最新版本的XML提取并重新组合成一个我可以使用的对象

这是一个订单看起来像XML的例子(您可以浏览一下 - 这正是我正在使用的):

<Order IsEmpty="N">
  <OrderNum>13903899</OrderNum>
  <AmtUOM>US$</AmtUOM>
  <CompanyUnit>Y</CompanyUnit>
  <CreatedBy>PenskeTruckLeasing</CreatedBy>
  <Opened>05/01/2018</Opened>
  <Priority>5</Priority>
  <ShopID>DE PERE</ShopID>
  <TaxOverride>F</TaxOverride>
  <UnitID>892</UnitID>
  <Vendor>64657200-7542</Vendor>
  <NonCompUnitID />
  <InvOrder IsEmpty="N">
    <InvType>REPAIR</InvType>
    <PONum>7542-136815</PONum>
  </InvOrder>
  <RepOrder IsEmpty="N">
    <RepSite>FACILITY</RepSite>
    <RepClass>NONSCHEDULED</RepClass>
    <Opened>05/01/2018</Opened>
    <Closed>05/02/2018</Closed>
  </RepOrder>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>001</SystemCode>
    <AssemblyCode>001</AssemblyCode>
    <PartCode>219</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>001</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>AC FILTER</PTLComplaint>
    <PTLCause>PLUGGED</PTLCause>
    <PTLCorrection>REPLACE FILTER AIR CONDITIONER</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>AC FILTER;AIR CONDITIONING, HEATING   VENTILATING SYSTEM;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>14.99</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>001</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>219</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>001</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>AIR FILTER PANEL</Descrip>
      <Hours>0</Hours>
      <LineTotal>14.99</LineTotal>
      <LineType>PART</LineType>
      <Mechanic />
      <PartID>AF55777</PartID>
      <QtyReqd>1.0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>31.02</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>001</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>219</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>001</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>31.02</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>1.37</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>001</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>219</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>001</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SALES TAX</Descrip>
      <Hours />
      <LineTotal>1.37</LineTotal>
      <LineType>TAX</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>1</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>002</SystemCode>
    <AssemblyCode>010</AssemblyCode>
    <PartCode>055</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>002</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>MIRROR SWITCH</PTLComplaint>
    <PTLCause>FAILED</PTLCause>
    <PTLCorrection>REPLACE MIRROR POWER DIRECTIONAL CONTROL SWITCH</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>MIRROR SWITCH;CAB   SHEET METAL;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>43.51</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>010</AssemblyCode>
      <PartCode>055</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SWITCH,MOTOR,REMOTE CONTR...</Descrip>
      <Hours>0</Hours>
      <LineTotal>43.51</LineTotal>
      <LineType>PART</LineType>
      <Mechanic />
      <PartID>DTL 15829700</PartID>
      <QtyReqd>1.0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>38.77</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>010</AssemblyCode>
      <PartCode>055</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>38.77</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>3.98</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>010</AssemblyCode>
      <PartCode>055</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SALES TAX</Descrip>
      <Hours />
      <LineTotal>3.98</LineTotal>
      <LineType>TAX</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>1</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>002</SystemCode>
    <AssemblyCode>035</AssemblyCode>
    <PartCode>015</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>002</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>WIPERS</PTLComplaint>
    <PTLCause>STREAK</PTLCause>
    <PTLCorrection>REPLACE WINDSHIELD WIPER BLADE</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>WIPERS;CAB   SHEET METAL;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>3.36</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>035</AssemblyCode>
      <PartCode>015</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>BLADE,WIPER</Descrip>
      <Hours>0</Hours>
      <LineTotal>6.72</LineTotal>
      <LineType>PART</LineType>
      <Mechanic />
      <PartID>ABP N82 7622</PartID>
      <QtyReqd>2.0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>23.26</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>035</AssemblyCode>
      <PartCode>015</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>23.26</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>.61</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>035</AssemblyCode>
      <PartCode>015</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SALES TAX</Descrip>
      <Hours />
      <LineTotal>.61</LineTotal>
      <LineType>TAX</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>1</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>002</SystemCode>
    <AssemblyCode>042</AssemblyCode>
    <PartCode>001</PartCode>
    <WorkAccomplishedCode>024</WorkAccomplishedCode>
    <CompCode>002</CompCode>
    <ActionCode>15</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>DECK PLATE</PTLComplaint>
    <PTLCause>LOSE</PTLCause>
    <PTLCorrection>REPAIR DECK PLATE/CROSSWALK AND MOUNTING BRACKETS</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>DECK PLATE;REPAIR DECK PLATE/CROSSWALK AND MOUNTING BRACKETS;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>77.54</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>002</SystemCode>
      <AssemblyCode>042</AssemblyCode>
      <PartCode>001</PartCode>
      <WorkAccomplishedCode>024</WorkAccomplishedCode>
      <CompCode>002</CompCode>
      <ActionCode>15</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>.50</Hours>
      <LineTotal>38.77</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>017</SystemCode>
    <AssemblyCode>001</AssemblyCode>
    <PartCode>020</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>017</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>LEFT REAR DRIVE TIRE</PTLComplaint>
    <PTLCause>MISSING TREAD SECTION</PTLCause>
    <PTLCorrection>REPLACE DRIVE TIRE(S)-3RD AXLE LEFT</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>LEFT REAR DRIVE TIRE;REAR TIRES;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>46.52</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>017</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>020</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>017</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>46.52</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>034</SystemCode>
    <AssemblyCode>004</AssemblyCode>
    <PartCode>021</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>034</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>BACK UP LIGHT</PTLComplaint>
    <PTLCause>BLOWN</PTLCause>
    <PTLCorrection>REPLACE CLEARANCE/MARKER LIGHT CAB AND CHASSIS ONLY-REAR RT</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>BACK UP LIGHT;LIGHTING SYSTEM;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>31.02</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>034</SystemCode>
      <AssemblyCode>004</AssemblyCode>
      <PartCode>021</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>034</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>31.02</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>.33</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>034</SystemCode>
      <AssemblyCode>004</AssemblyCode>
      <PartCode>021</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>034</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SALES TAX</Descrip>
      <Hours />
      <LineTotal>.33</LineTotal>
      <LineType>TAX</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>1</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>3.59</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>034</SystemCode>
      <AssemblyCode>004</AssemblyCode>
      <PartCode>021</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>034</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LAMP,BACKUP</Descrip>
      <Hours>0</Hours>
      <LineTotal>3.59</LineTotal>
      <LineType>PART</LineType>
      <Mechanic />
      <PartID>TL  40204</PartID>
      <QtyReqd>1.0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>041</SystemCode>
    <AssemblyCode>001</AssemblyCode>
    <PartCode>005</PartCode>
    <WorkAccomplishedCode>003</WorkAccomplishedCode>
    <CompCode>041</CompCode>
    <ActionCode>10</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>LOW POWER</PTLComplaint>
    <PTLCause>FILTER</PTLCause>
    <PTLCorrection>COMPLETE AF - REPLACE AIR FILTER MAIN ENGINE (INCLUDES BOTH ON M2'S)</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>LOW POWER;AIR INTAKE SYSTEM;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>57.70</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>041</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>005</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>041</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>FILTER,ELEMENT,AIR FILTER</Descrip>
      <Hours>0</Hours>
      <LineTotal>57.70</LineTotal>
      <LineType>PART</LineType>
      <Mechanic />
      <PartID>AF27879</PartID>
      <QtyReqd>1.0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>31.02</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>041</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>005</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>041</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>1</Hours>
      <LineTotal>31.02</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>5.28</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>041</SystemCode>
      <AssemblyCode>001</AssemblyCode>
      <PartCode>005</PartCode>
      <WorkAccomplishedCode>003</WorkAccomplishedCode>
      <CompCode>041</CompCode>
      <ActionCode>10</ActionCode>
      <Descrip>SALES TAX</Descrip>
      <Hours />
      <LineTotal>5.28</LineTotal>
      <LineType>TAX</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>1</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
  <OrderSec IsEmpty="N">
    <Billable>Y</Billable>
    <SystemCode>053</SystemCode>
    <AssemblyCode>004</AssemblyCode>
    <PartCode>001</PartCode>
    <WorkAccomplishedCode>013</WorkAccomplishedCode>
    <CompCode>053</CompCode>
    <ActionCode>95</ActionCode>
    <Complaint>PM</Complaint>
    <CompltDate>05/02/2018 07:56:38 </CompltDate>
    <PTLComplaint>CAMPAIGN</PTLComplaint>
    <PTLCause>REQUIRED</PTLCause>
    <PTLCorrection>COMPLETE OTH KIDDE CAMPAIGN - BUL#50-117 - REPLACE FIRE EXTINGUISHER</PTLCorrection>
    <DelReason />
    <ExtWarranty>N</ExtWarranty>
    <Opened>05/01/2018 11:39:06 </Opened>
    <Priority>5</Priority>
    <RepReason>PM</RepReason>
    <SecComment>CAMPAIGN;EXPENDABLES;INV#:0013903899</SecComment>
    <SectionNum />
    <TaxOverride />
    <WarrType />
    <OrderLine IsEmpty="N">
      <AdditType />
      <AfterMarket />
      <ChgAmt>77.50</ChgAmt>
      <ChgCateg>STANDARD</ChgCateg>
      <ChgDate>05/02/2018 07:56:38 </ChgDate>
      <ChgUOM>US$</ChgUOM>
      <SystemCode>053</SystemCode>
      <AssemblyCode>004</AssemblyCode>
      <PartCode>001</PartCode>
      <WorkAccomplishedCode>013</WorkAccomplishedCode>
      <CompCode>053</CompCode>
      <ActionCode>95</ActionCode>
      <Descrip>LABOR</Descrip>
      <Hours>.10</Hours>
      <LineTotal>7.75</LineTotal>
      <LineType>LABOR</LineType>
      <Mechanic />
      <PartID />
      <QtyReqd>0</QtyReqd>
      <QtyUOM />
      <Taxable />
      <TaxAmt />
      <VendorLine />
    </OrderLine>
  </OrderSec>
</Order>

现在,一个典型的订单包含大约60个这样的部分。长话短说,我需要从这些部分重新组装XML。

到目前为止,我已经创建了xml对象并创建了第一个节点(这些节点只是该文件中的一个),这是VendorInvoice。

# new xml
[xml]$Newxml = New-Object System.Xml.XmlDocument
$newxml.LoadXml("<?xml version=`"1.0`" encoding=`"us-ascii`"?><VendorInvoices></VendorInvoices>")

我需要将这些订单中的每一个附加到VendorInvoice到VendorInvoice节点,我试图在这里做:

foreach($VendorInvoice in $xml.VendorInvoices)
{
    foreach($Order in $VendorInvoices.Order)
    {
        $newxml.AppendChild($Order.OuterXml)
        $newxml.Save($Newxml)
    }
}

但是,这样做似乎没有在VendorInvoices节点下填充任何内容:

xml                               VendorInvoices
---                               --------------
version="1.0" encoding="us-ascii" 

有谁知道我在哪里出错?这个过程要求我从XML文件中删除很多东西,但这是我必须构建XML文件的第一个例子。

如果需要,我可以提供更多信息。

1 个答案:

答案 0 :(得分:1)

这是一个简化输入的工作示例:

# The input document.
[xml] $xml = @'
<VendorInvoices>
  <Order>
    <OrderNum>1</OrderNum>
  </Order>
  <Order>
    <OrderNum>2</OrderNum>
  </Order>
</VendorInvoices>
'@

# Create the target XML document.
# Add a <dummy /> child element to <VendorVoices>, so we
# can get a reference to child element <VendorVoices> with dot notation below.
[xml] $newXml = @'
<?xml version="1.0" encoding="us-ascii"?>
<VendorInvoices><dummy/></VendorInvoices>
'@

# Get a referene to the new document's <VendorInvoices> element.
$newVendorInvoices = $newXml.VendorInvoices
$newVendorInvoices.Removeall() # Remove the dummy child node.

# Loop over all <Order> child elements of the original document's
# <VendorInvoices> element and add them to the target document's
# <VendorInvoices> element.
foreach ($order in $xml.VendorInvoices.Order) {
  # Import the order at hand into the new document and append it to the
  # <VendorInvoices> element. Note: .AppendChild() modifies the document in-place
  # and *also returns* the new child node; we assign to $null to suppress this output.
  $null = $newVendorInvoices.AppendChild($newXml.ImportNode($order, $true))
}

# Use Format-Custom to print a quick-and-dirty visualization of the document. 
# If you want to save the document to a new file, use, e.g., $newXml.Save("$PWD/new.xml")
$newXml | Format-Custom

以上产量:

class XmlDocument
{
  xml = version="1.0" encoding="us-ascii"
  VendorInvoices = 
    class XmlElement
    {
      Order = 
        [
          class XmlElement
          {
            OrderNum = 1
          }
          class XmlElement
          {
            OrderNum = 2
          }
        ]

    }
}

至于您尝试的内容

  • 你的内部循环有一个拼写错误:$VendorInvoices应为$VendorInvoice

  • 您错误地尝试将$Order附加到目标文档的 root (document)元素而不是子元素<VendorInvoices>。< / p>

  • 使用.OuterXml(需要[string]个实例)将现有元素的.AppendChild()属性值 - [System.Xml.XmlNode]附加到另一个元素上并且# 39;工作。

  • $newXml.Save($newXml)毫无意义:内存中 DOM操作不需要保存操作,如果要保存到文件,您必须指定输出文件路径;实际上,它是$newXml的字符串化 - 这是字符串文字'System.Xml.XmlDocument' - 在此处被解释为输出文件名。