[英]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"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.