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

Append an XML file to another using LINQ TO XML in C#

我有两个 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>

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

class Program
{
    static void Main(string[] args)
    {
        XNamespace ns = "https://mws.amazonservices.com/Orders/2013-09-01";

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

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

因为 xml 有命名空间,所以我的代码工作正常,抱歉,我将 XDocument 更改为 XElement

string xml1 = @"<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>";
    string xml2 = @"<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>";
    var root1 = XElement.Parse(xml1);
    var root2 = XElement.Parse(xml2);
    root1.Elements(root1.GetDefaultNamespace() + "ListOrderItemsResult").Last().AddAfterSelf(root2.Elements(root2.GetDefaultNamespace() + "ListOrderItemsResult"));