代码之家  ›  专栏  ›  技术社区  ›  Matt

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

  •  1
  • Matt  · 技术社区  · 6 年前

    我目前正在研究一些XML文件的版本控制功能。基本的工作流程是,当XML文件被解析时,按顺序将其分解,然后放入数据库表中,然后根据最新版本的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 回复  |  直到 6 年前
        1
  •  1
  •   mklement0    6 年前

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

    # 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 (文档)目标文档的元素,而不是子元素 <VendorInvoices> .

    • 附加 .OuterXml 属性值-A [string] -使用 .AppendChild() -它期望 [System.Xml.XmlNode] 实例-到其他元素不起作用。

    • $newXml.Save($newXml) 毫无意义:不需要对 在记忆中 dom操作,如果要保存到 文件 ,必须指定 输出文件路径 ;事实上,它是 $newXml -这是字符串文字 'System.Xml.XmlDocument' -在这里被解释为输出文件名。