none
Swapping XML Nodes

    Question

  • Hi
    I need to change the order of 2 nodes in an xml file. Here is the file i recieve. I can do this in VB.Net or VB6
    <LabelExport>
    <Shipment>
    <EPPName><![CDATA[Customer(Vendor)]]></EPPName>
    <TemplateName><![CDATA[Customer__To_]]></TemplateName>
    <NestedTemplateName><![CDATA[Customer__To__Nested]]></NestedTemplateName>
    <UCCLoopID><![CDATA[101]]></UCCLoopID>
    <SenderID><![CDATA[Phone - 5555551212]]></SenderID>
    <ReceiverID><![CDATA[Phone - 5555551212T]]></ReceiverID>
    <ShippingShipmentID><![CDATA[15600]]></ShippingShipmentID>
    <Routing><![CDATA[FEDC]]></Routing>
    <SCAC><![CDATA[FEDX]]></SCAC>
    <NumofCartonsorPalletsShipped><![CDATA[2]]></NumofCartonsorPalletsShipped>
    <ShippingWeight><![CDATA[0]]></ShippingWeight>
    <ShipFromName><![CDATA[Company LLC]]></ShipFromName>
    <ShipFromAddress1><![CDATA[1234 Main DRIVE]]></ShipFromAddress1>
    <ShipFromCity><![CDATA[ANYWHERE]]></ShipFromCity>
    <ShipFromState><![CDATA[OH]]></ShipFromState>
    <ShipFromZip><![CDATA[55555]]></ShipFromZip>
    <ShipToAddressCode><![CDATA[1316]]></ShipToAddressCode>
    <Order>
    <PONumber><![CDATA[XP94083]]></PONumber>
    <InvoiceNumber><![CDATA[163831]]></InvoiceNumber>
    <MSLStoreNumber><![CDATA[1316]]></MSLStoreNumber>
    <MSLStoreName><![CDATA[HADLEY]]></MSLStoreName>
    <MSLStoreAddress1><![CDATA[337 RUSSELL STREET]]></MSLStoreAddress1>
    <MSLStoreCity><![CDATA[HADLEY ]]></MSLStoreCity>
    <MSLStoreState><![CDATA[NA]]></MSLStoreState>
    <MSLStoreZip><![CDATA[00000]]></MSLStoreZip>
    <OrderQuantityLading><![CDATA[2]]></OrderQuantityLading>
    <OrderIndex><![CDATA[1]]></OrderIndex>
    <OrderCount><![CDATA[1]]></OrderCount>
    <Pack>
    <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
    <VendorNumber><![CDATA[40759]]></VendorNumber>
    <UCC128Number><![CDATA[00008347420000290470]]></UCC128Number>
    <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
    <UCC128Suffix><![CDATA[0000290470]]></UCC128Suffix>
    <Misc2><![CDATA[6]]></Misc2>
    <Index><![CDATA[1]]></Index>
    <PackIndex><![CDATA[1]]></PackIndex>
    <PackCount><![CDATA[2]]></PackCount>
    <Item>
    <UPCNumber><![CDATA[834742000031]]></UPCNumber>
    <ItemDescription><![CDATA[3.5 qt Stainless Steel]]></ItemDescription>
    <ItemVendorIDNumber><![CDATA[660104B]]></ItemVendorIDNumber>
    <Qty><![CDATA[6]]></Qty>
    </Item>
    </Pack>
    <Pack>
    <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
    <VendorNumber><![CDATA[40759]]></VendorNumber>
    <UCC128Number><![CDATA[00008347420000290487]]></UCC128Number>
    <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
    <UCC128Suffix><![CDATA[0000290487]]></UCC128Suffix>
    <Misc2><![CDATA[6]]></Misc2>
    <Index><![CDATA[2]]></Index>
    <PackIndex><![CDATA[2]]></PackIndex>
    <PackCount><![CDATA[2]]></PackCount>
    <Item>
    <UPCNumber><![CDATA[834742000024]]></UPCNumber>
    <ItemDescription><![CDATA[5.5 qt Stainless Steel]]></ItemDescription>
    <ItemVendorIDNumber><![CDATA[660105B]]></ItemVendorIDNumber>
    <Qty><![CDATA[6]]></Qty>
    </Item>
    </Pack>
    </Order>
    </Shipment>
    </LabelExport>

    I need the item node to change places with the pack node like this
    There are multiple shipments in the whole file I have only included one here.
    <LabelExport>
    <Shipment>
    <EPPName><![CDATA[Customer(Vendor)]]></EPPName>
    <TemplateName><![CDATA[Customer__To_]]></TemplateName>
    <NestedTemplateName><![CDATA[Customer__To__Nested]]></NestedTemplateName>
    <UCCLoopID><![CDATA[101]]></UCCLoopID>
    <SenderID><![CDATA[Phone - 5555551212]]></SenderID>
    <ReceiverID><![CDATA[Phone - 5555551212T]]></ReceiverID>
    <ShippingShipmentID><![CDATA[15600]]></ShippingShipmentID>
    <Routing><![CDATA[FEDC]]></Routing>
    <SCAC><![CDATA[FEDX]]></SCAC>
    <NumofCartonsorPalletsShipped><![CDATA[2]]></NumofCartonsorPalletsShipped>
    <ShippingWeight><![CDATA[0]]></ShippingWeight>
    <ShipFromName><![CDATA[Company LLC]]></ShipFromName>
    <ShipFromAddress1><![CDATA[1234 Main DRIVE]]></ShipFromAddress1>
    <ShipFromCity><![CDATA[ANYWHERE]]></ShipFromCity>
    <ShipFromState><![CDATA[OH]]></ShipFromState>
    <ShipFromZip><![CDATA[55555]]></ShipFromZip>
    <ShipToAddressCode><![CDATA[1316]]></ShipToAddressCode>
    <Order>
    <PONumber><![CDATA[XP94083]]></PONumber>
    <InvoiceNumber><![CDATA[163831]]></InvoiceNumber>
    <MSLStoreNumber><![CDATA[1316]]></MSLStoreNumber>
    <MSLStoreName><![CDATA[HADLEY]]></MSLStoreName>
    <MSLStoreAddress1><![CDATA[337 RUSSELL STREET]]></MSLStoreAddress1>
    <MSLStoreCity><![CDATA[HADLEY ]]></MSLStoreCity>
    <MSLStoreState><![CDATA[NA]]></MSLStoreState>
    <MSLStoreZip><![CDATA[00000]]></MSLStoreZip>
    <OrderQuantityLading><![CDATA[2]]></OrderQuantityLading>
    <OrderIndex><![CDATA[1]]></OrderIndex>
    <OrderCount><![CDATA[1]]></OrderCount>
    <Item>
    <UPCNumber><![CDATA[834742000031]]></UPCNumber>
    <ItemDescription><![CDATA[3.5 qt Stainless Steel]]></ItemDescription>
    <ItemVendorIDNumber><![CDATA[660104B]]></ItemVendorIDNumber>
    <Qty><![CDATA[6]]></Qty>
    <Pack>
    <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
    <VendorNumber><![CDATA[40759]]></VendorNumber>
    <UCC128Number><![CDATA[00008347420000290470]]></UCC128Number>
    <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
    <UCC128Suffix><![CDATA[0000290470]]></UCC128Suffix>
    <Misc2><![CDATA[6]]></Misc2>
    <Index><![CDATA[1]]></Index>
    <PackIndex><![CDATA[1]]></PackIndex>
    <PackCount><![CDATA[2]]></PackCount>
    </Item>
    </Pack>
    <Item>
    <UPCNumber><![CDATA[834742000024]]></UPCNumber>
    <ItemDescription><![CDATA[5.5 qt Stainless Steel]]></ItemDescription>
    <ItemVendorIDNumber><![CDATA[660105B]]></ItemVendorIDNumber>
    <Qty><![CDATA[6]]></Qty>
    <Pack>
    <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
    <VendorNumber><![CDATA[40759]]></VendorNumber>
    <UCC128Number><![CDATA[00008347420000290487]]></UCC128Number>
    <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
    <UCC128Suffix><![CDATA[0000290487]]></UCC128Suffix>
    <Misc2><![CDATA[6]]></Misc2>
    <Index><![CDATA[2]]></Index>
    <PackIndex><![CDATA[2]]></PackIndex>
    <PackCount><![CDATA[2]]></PackCount>
    </Item>
    </Pack>
    </Order>
    </Shipment>
    </LabelExport>
    Saturday, November 28, 2009 12:04 PM

Answers

  • In the original XML you posted the Item element is a child of the Pack element. The target markup you posted is not even well-formed XML so it is not clear what you want to achieve.
    The following .NET 3.5 VB.NET code

            Dim doc As XDocument = XDocument.Load("input.xml")
            For Each pack As XElement In doc.Root.<Shipment>.<Order>.<Pack>.ToList()
                Dim item As XElement = pack.<Item>(0)
                item.Add(New XElement(pack.Name, pack.Elements().Except(New XElement() {item})))
                pack.ReplaceWith(item)
            Next
    
            doc.Save("output.xml")
    creates the output markup

    <LabelExport>
      <Shipment>
        <EPPName><![CDATA[Customer(Vendor)]]></EPPName>
        <TemplateName><![CDATA[Customer__To_]]></TemplateName>
        <NestedTemplateName><![CDATA[Customer__To__Nested]]></NestedTemplateName>
        <UCCLoopID><![CDATA[101]]></UCCLoopID>
        <SenderID><![CDATA[Phone - 5555551212]]></SenderID>
        <ReceiverID><![CDATA[Phone - 5555551212T]]></ReceiverID>
        <ShippingShipmentID><![CDATA[15600]]></ShippingShipmentID>
        <Routing><![CDATA[FEDC]]></Routing>
        <SCAC><![CDATA[FEDX]]></SCAC>
        <NumofCartonsorPalletsShipped><![CDATA[2]]></NumofCartonsorPalletsShipped>
        <ShippingWeight><![CDATA[0]]></ShippingWeight>
        <ShipFromName><![CDATA[Company LLC]]></ShipFromName>
        <ShipFromAddress1><![CDATA[1234 Main DRIVE]]></ShipFromAddress1>
        <ShipFromCity><![CDATA[ANYWHERE]]></ShipFromCity>
        <ShipFromState><![CDATA[OH]]></ShipFromState>
        <ShipFromZip><![CDATA[55555]]></ShipFromZip>
        <ShipToAddressCode><![CDATA[1316]]></ShipToAddressCode>
        <Order>
          <PONumber><![CDATA[XP94083]]></PONumber>
          <InvoiceNumber><![CDATA[163831]]></InvoiceNumber>
          <MSLStoreNumber><![CDATA[1316]]></MSLStoreNumber>
          <MSLStoreName><![CDATA[HADLEY]]></MSLStoreName>
          <MSLStoreAddress1><![CDATA[337 RUSSELL STREET]]></MSLStoreAddress1>
          <MSLStoreCity><![CDATA[HADLEY ]]></MSLStoreCity>
          <MSLStoreState><![CDATA[NA]]></MSLStoreState>
          <MSLStoreZip><![CDATA[00000]]></MSLStoreZip>
          <OrderQuantityLading><![CDATA[2]]></OrderQuantityLading>
          <OrderIndex><![CDATA[1]]></OrderIndex>
          <OrderCount><![CDATA[1]]></OrderCount>
          <Item>
            <UPCNumber><![CDATA[834742000031]]></UPCNumber>
            <ItemDescription><![CDATA[3.5 qt Stainless Steel]]></ItemDescription>
            <ItemVendorIDNumber><![CDATA[660104B]]></ItemVendorIDNumber>
            <Qty><![CDATA[6]]></Qty>
            <Pack>
              <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
              <VendorNumber><![CDATA[40759]]></VendorNumber>
              <UCC128Number><![CDATA[00008347420000290470]]></UCC128Number>
              <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
              <UCC128Suffix><![CDATA[0000290470]]></UCC128Suffix>
              <Misc2><![CDATA[6]]></Misc2>
              <Index><![CDATA[1]]></Index>
              <PackIndex><![CDATA[1]]></PackIndex>
              <PackCount><![CDATA[2]]></PackCount>
            </Pack>
          </Item>
          <Item>
            <UPCNumber><![CDATA[834742000024]]></UPCNumber>
            <ItemDescription><![CDATA[5.5 qt Stainless Steel]]></ItemDescription>
            <ItemVendorIDNumber><![CDATA[660105B]]></ItemVendorIDNumber>
            <Qty><![CDATA[6]]></Qty>
            <Pack>
              <ShippingPackQuantity><![CDATA[6]]></ShippingPackQuantity>
              <VendorNumber><![CDATA[40759]]></VendorNumber>
              <UCC128Number><![CDATA[00008347420000290487]]></UCC128Number>
              <UCC128Prefix><![CDATA[0000834742]]></UCC128Prefix>
              <UCC128Suffix><![CDATA[0000290487]]></UCC128Suffix>
              <Misc2><![CDATA[6]]></Misc2>
              <Index><![CDATA[2]]></Index>
              <PackIndex><![CDATA[2]]></PackIndex>
              <PackCount><![CDATA[2]]></PackCount>
            </Pack>
          </Item>
        </Order>
      </Shipment>
    </LabelExport>
    
    If that is not what you want then please post a well-formed XML sample showing the result you want.
    MVP XML My blog
    Saturday, November 28, 2009 1:14 PM