-2

I have two XML files: orderlist.xml

  <ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
      <ListOrderItemsResult>
        <AmazonOrderId>102-7574501-6521843</AmazonOrderId>
        <OrderItems>
          <OrderItem>
            <ASIN>B00PBOOX6C</ASIN>
            <SellerSKU>OJ-J0CO-4SEH</SellerSKU>
            <OrderItemId>03136321944794</OrderItemId>
            <Title>Nitric Acid 70% , ACS, REAGENT Grade, Alliance Chemical Brand, 2.5 Liter(7 Lbs) Bottle</Title>
            <QuantityOrdered>1</QuantityOrdered>
            <QuantityShipped>0</QuantityShipped>
            <ItemPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>108.00</Amount>
            </ItemPrice>
            <ShippingPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>27.00</Amount>
            </ShippingPrice>
            <GiftWrapPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </GiftWrapPrice>
            <ItemTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ItemTax>
            <ShippingTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ShippingTax>
            <GiftWrapTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </GiftWrapTax>
            <ShippingDiscount>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ShippingDiscount>
            <PromotionDiscount>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </PromotionDiscount>
            <PromotionIds />
            <ConditionId>New</ConditionId>
            <ConditionSubtypeId>New</ConditionSubtypeId>
          </OrderItem>
        </OrderItems>
      </ListOrderItemsResult>
      <ResponseMetadata>
        <RequestId>2b407071-501d-45a2-9baa-2f5ec24f61e7</RequestId>
      </ResponseMetadata>
    </ListOrderItemsResponse>

and orderlist2.xml:

    <ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
      <ListOrderItemsResult>
        <AmazonOrderId>112-5324115-6814611</AmazonOrderId>
        <OrderItems>
          <OrderItem>
            <ASIN>B00PGF7U6G</ASIN>
            <SellerSKU>60-JOUW-Q8HA</SellerSKU>
            <OrderItemId>19976596962514</OrderItemId>
            <Title>Nitric Acid 70% , ACS REAGENT GRADE, 2.5 Liter(7 Lbs) Bottle</Title>
            <QuantityOrdered>1</QuantityOrdered>
            <QuantityShipped>0</QuantityShipped>
            <ItemPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>108.00</Amount>
            </ItemPrice>
            <ShippingPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>27.00</Amount>
            </ShippingPrice>
            <GiftWrapPrice>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </GiftWrapPrice>
            <ItemTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ItemTax>
            <ShippingTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ShippingTax>
            <GiftWrapTax>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </GiftWrapTax>
            <ShippingDiscount>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </ShippingDiscount>
            <PromotionDiscount>
              <CurrencyCode>USD</CurrencyCode>
              <Amount>0.00</Amount>
            </PromotionDiscount>
            <PromotionIds />
            <ConditionId>New</ConditionId>
            <ConditionSubtypeId>New</ConditionSubtypeId>
          </OrderItem>
        </OrderItems>
      </ListOrderItemsResult>
      <ResponseMetadata>
        <RequestId>e9e67246-c07d-4bf0-825b-929cd37c694f</RequestId>
      </ResponseMetadata>
    </ListOrderItemsResponse>

The combined XML file will look at like this:

<AmazonOrderId>112-5324115-6814611</AmazonOrderId>
<OrderItems>
    <OrderItem>
        <ASIN>B00PGF7U6G</ASIN>
        <SellerSKU>60-JOUW-Q8HA</SellerSKU>
        <OrderItemId>19976596962514</OrderItemId>
        <Title>Nitric Acid 70% , ACS REAGENT GRADE, 2.5 Liter(7 Lbs) Bottle</Title>
        <QuantityOrdered>1</QuantityOrdered>
        <QuantityShipped>0</QuantityShipped>
        <ItemPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>108.00</Amount>
        </ItemPrice>
        <ShippingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>27.00</Amount>
        </ShippingPrice>
        <GiftWrapPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </GiftWrapPrice>
        <ItemTax>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </ItemTax>
        <ShippingTax>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </ShippingTax>
        <GiftWrapTax>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </GiftWrapTax>
        <ShippingDiscount>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </ShippingDiscount>
        <PromotionDiscount>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
        </PromotionDiscount>
        <PromotionIds />
        <ConditionId>New</ConditionId>
        <ConditionSubtypeId>New</ConditionSubtypeId>
    </OrderItem>
    <OrderItem>
        <Title>Nitric Acid 70% , ACS REAGENT GRADE, 2.5 Liter(7 Lbs)</Title>
        <QuantityOrdered>1</QuantityOrdered>
        <ItemPrice>
            <Amount>108.00</Amount>
        </ItemPrice>
        <ShippingPrice>
            <Amount>27.00</Amount>
        </ShippingPrice>
    </OrderItem>
</OrderItems>undefined</ListOrderItemsResult>undefined<ResponseMetadata>
<RequestId>e9e67246-c07d-4bf0-825b-929cd37c694f</RequestId>undefined</ResponseMetadata>

I want to append the 2nd file to the 1st file, but the only elements I want from the second file are <Title>, <QuantityOrdered>, <ItemPrice>, <Amount>, <ShippingPrice>, <Amount>. I think think the parent element is and the data should be encapsulated within the <OrderItem> tag. How can I do this?

Daniel Taki
  • 93
  • 1
  • 3
  • 13
  • 2
    If you do a straight append, it won't be valid XML. – PC Luddite Jul 13 '15 at 20:05
  • Parse the XML, combine the contents and write it out. – cbr Jul 13 '15 at 20:06
  • @cubrr I think this doesn't make sense at all. Combining the contents may result also in a wrong result. Maybe it's valid xml, but the structure looses its sense. At first I thought you could combine all OrderItem in one OrderItems nodes, but then you would loose the unique AmazonOrderId. So one must go one node up in the tree. That would be ListOrderItemsResult. But as the name implies, its not a list. So combining to orders in one doc, shouldn't be possible without loosing the semantic sense. – netblognet Jul 13 '15 at 20:23
  • It's not clear what you expect the result to look like - as has been said, you can't just slam these two together. – Charles Mager Jul 13 '15 at 20:52
  • Why won't this be valid XML? I have a amazon API that grabs only one order at a time per call. But I need to gather all the orders, so I need to iterate to call the function and return the XML string then append it to the file each time - to create a list of orders. – Daniel Taki Jul 14 '15 at 03:52
  • LastOrDefault is returning null. Why? Also, don't edit a new question into an old question, just go ask a new one. –  Jul 15 '15 at 19:32

1 Answers1

1

If you just read the files and append one to the other, it won't be valid Xml. I'm assuming you mean taking all of the child nodes of the root element from one, and append them to the other. This means you have to load both XmlDocuments, and import the nodes from the second document to the first one.

using System.Xml;

class Program
{
    static void Main(string[] args) 
    {
        XmlDocument list1 = new XmlDocument();
        XmlDocument list2 = new XmlDocument();
        list1.Load("orderlist.xml");
        list2.Load("orderlist2.xml");
        foreach (XmlNode childOfSecond in list2.DocumentElement.ChildNodes)
        {
            XmlNode childOfFirst = list1.ImportNode(childOfSecond, true);
            list1.DocumentElement.AppendChild(childOfFirst);
        }
        list1.Save("orderlist3.xml");
    }
}
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • This is going to end up with quite a weird XML structure (it basically 'embeds' the second file into the first). I don't think it's particularly clear what's required here. As an aside, I'd advise against using `XmlDocument` unless you have a specific need - LINQ to XML is a much better API. – Charles Mager Jul 13 '15 at 20:53
  • @CharlesMager You're right, I'm not exactly sure what he needs to do, but this seemed like a simple straight forward solution. Anyway, I've never used the LINQ to XML API, and I haven't really looked into the pros and cons (I've been using XmlDocument since the days before Linq, and old habits die hard, I suppose). – PC Luddite Jul 13 '15 at 22:28
  • I'm trying to create one file with all my orders since the amazon API only allows me to request one item at a time. To do this I need to iterate the function call - asking the user to enter the order id - for each product. – Daniel Taki Jul 14 '15 at 04:54
  • @DanielTaki Well, you probably want to specify a new root node, and make each xml document its own child node. – PC Luddite Jul 14 '15 at 04:57
  • @CharlesMager Yah you're right about trying to slam these two together because I tried it and it threw an error. I don't need the entire 2nd file. I just want the ``, `<quantityordered>`, `<itemprice>`, `<amount>`, `<shippingprice>`, `<amount>` tags.</amount></shippingprice></amount></itemprice></quantityordered> – Daniel Taki Jul 14 '15 at 18:37
  • Reading a related question that was answered that looks similar to what I want to do maybe I can figure it out. – Daniel Taki Jul 14 '15 at 18:39
  • I tried using this code: `var xml1 = XDocument.Load("Orderlist.xml"); var xml2 = XDocument.Load("Orderlist2.xml"); ` `xml1.Descendants("ListOrderItemsResult").LastOrDefault().AddAfterSelf(xml2.Descendants("ListOrderItemsResult"));` `xml1.Save("xml1.xml");` Getting this error: An unhandled exception of type 'System.NullReferenceException' occurred in Concat.exe Additional information: Object reference not set to an instance of an object. – Daniel Taki Jul 14 '15 at 20:32
  • @ThePcLuddite I followed the answer in another question similar to mine but I'm still making some mistake because an exception is thrown. – Daniel Taki Jul 14 '15 at 20:52
  • @DanielTaki This question has been closed. You might want to try to ask another one, but make sure you include all relevant parts and are clear with what you're asking. – PC Luddite Jul 15 '15 at 01:14