使用C#中的LINQ TO XML将XML文件附加到另一个XML文件

时间:2015-07-15 15:26:41

标签: c# xml linq xml-parsing

我有两个XML文件,我想将Orderlist2.xml追加到Orderlist.xml

这是我要实现的代码,但每次运行它都会出错: An unhandled exception of type 'System.NullReferenceException' occurred in Concat.exe Additional information: Object reference not set to an instance of an object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.IO;

namespace Concat
{
    class Program
    {
        static void Main(string[] args)
        {

            var xml1 = XDocument.Load("Orderlist.xml");
            var xml2 = XDocument.Load("Orderlist2.xml");


            xml1.Descendants("ListOrderItemsResult").LastOrDefault().AddAfterSelf(xml2.Descendants("ListOrderItemsResult"));
            xml1.Save("Orderlist.xml");
        }
    }
}

Orderlist.xml

<ListOrderItemsResponse
    xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
    <ListOrderItemsResult>
        <AmazonOrderId>115-8324109-2580214</AmazonOrderId>
        <OrderItems>
            <OrderItem>
                <ASIN>B00CXWANX2</ASIN>
                <SellerSKU>UJ-D11Y-CDD4</SellerSKU>
                <OrderItemId>36597396932386</OrderItemId>
                <Title>Isopropyl Alcohol 99% Technical Grade in one Gallon Poly Bottle.</Title>
                <QuantityOrdered>2</QuantityOrdered>
                <QuantityShipped>0</QuantityShipped>
                <ItemPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>27.60</Amount>
                </ItemPrice>
                <ShippingPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>54.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>92b66d69-c0e1-4549-9122-7846faf06644</RequestId>
    </ResponseMetadata>
</ListOrderItemsResponse>

Orderlist2.xml

<ListOrderItemsResponse
    xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
    <ListOrderItemsResult>
        <AmazonOrderId>104-0314222-2069825</AmazonOrderId>
        <OrderItems>
            <OrderItem>
                <ASIN>B00D6NJRCU</ASIN>
                <SellerSKU>CF-IVS1-49JR</SellerSKU>
                <OrderItemId>36825199299522</OrderItemId>
                <Title>Titanium Dioxide, Five Lb Bag</Title>
                <QuantityOrdered>1</QuantityOrdered>
                <QuantityShipped>0</QuantityShipped>
                <ItemPrice>
                    <CurrencyCode>USD</CurrencyCode>
                    <Amount>12.49</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>0d81935c-2bde-4e46-8f4f-7d25cc13d322</RequestId>
    </ResponseMetadata>
</ListOrderItemsResponse>

1 个答案:

答案 0 :(得分:0)

我认为你的问题是XML中的命名空间,应该这样做:

0 0