我有两个XML文件: 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>
和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>
组合的XML文件将如下所示:
<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>
我想将第二个文件附加到第一个文件,但我想从第二个文件中找到的唯一元素是<Title>
,<QuantityOrdered>
,<ItemPrice>
,<Amount>
, <ShippingPrice>
,<Amount>
。我认为认为父元素是,并且数据应该封装在<OrderItem>
标记内。我怎么能这样做?
答案 0 :(得分:1)
如果您只是读取文件并将其追加到另一个文件中,则它不会是有效的Xml。我假设您的意思是从根元素中获取根元素的所有子节点,并将它们附加到另一个节点。这意味着您必须加载XmlDocument
,并将第二个文档中的节点导入第一个文档。
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");
}
}