将多个XML文件合并到一个XML中

时间:2016-02-07 12:49:57

标签: c# xml

我有大量的XML,我想从所有这些XML中生成1个XML(格式相同)。

这是1.XML

的一个例子
<?xml version="1.0" encoding="UTF-8"?>
<RootDTO xmlns:json='http://james.newtonking.com/projects/json'>
   <destination>
      <name>xxx</name>
   </destination>
   <orderData>                                         
      <items json:Array='true'>  
            <shipmentIndex Name="items"></shipmentIndex>
            <barcode>12345</barcode>                                                                                                       
      </items>  
      <misCode>9876543210</misCode>                                      
          <shipments>                                      
                <sourceShipmentId></sourceShipmentId>      
                <shipmentIndex Name="shipments"></shipmentIndex>                         
          </shipments>                         
   </orderData>
</RootDTO>

这个代表结合1.xml2.xml之后的Result.xml - (看起来与1.xml完全相同

<!-- Represent Result.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<RootDTO xmlns:json='http://james.newtonking.com/projects/json'>
   <destination>
      <name>xxx</name>
   </destination>
   <orderData>                                         
      <items json:Array='true'>  
            <shipmentIndex Name="items"></shipmentIndex>
            <barcode>12345</barcode>                                                                                                       
      </items>  

    <items json:Array='true'>     <!-- from 2.xml-->
            <shipmentIndex Name="items"></shipmentIndex>
            <barcode>12345</barcode>                                                                                                       
      </items>  


      <misCode>9876543210</misCode>                                      
          <shipments>                                      
                <sourceShipmentId></sourceShipmentId>      
                <shipmentIndex Name="shipments"></shipmentIndex>                         
          </shipments>     


      <shipments>       <!--From 2.xml-->                              
                <sourceShipmentId></sourceShipmentId>      
                <shipmentIndex Name="shipments"></shipmentIndex>                         
          </shipments>                             
   </orderData>
</RootDTO>

这样做的目的是将多个货件(一起,它们是1个订单)组合成1个代表完整订单的XML。

像这样的伪:

for(i=0; i< xmls.count()< i++)
{
   itemsElement.appendTo(xml[i].items);
   shipmentsElement.appendTo(xml[i].shipments);
}

我想要实现的是追加元素 - “商品”和“货件” 到1个代表订单的XML。

我使用此代码来获取XML strng:var xml = XElement.Parse(renderedOutput);代表我的XML字符串

1 个答案:

答案 0 :(得分:1)

为了简化示例代码,我将相同的xml三次添加到同一文档中。您可以使用Load方法而不是Parse来从文件而不是字符串中获取xml。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const int NUMBER_OF_XML = 3;
        static void Main(string[] args)
        {
            string xml =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                     "<RootDTO xmlns:json='http://james.newtonking.com/projects/json'>" +
                       "<destination>" +
                          "<name>xxx</name>" +
                       "</destination>" +
                       "<orderData>" +
                          "<items json:Array='true'>" +
                                "<shipmentIndex Name=\"items\"></shipmentIndex>" +
                                "<barcode>12345</barcode>" +
                          "</items>" +
                          "<misCode>9876543210</misCode>" +
                              "<shipments>" +
                                    "<sourceShipmentId></sourceShipmentId>" +
                                    "<shipmentIndex Name=\"shipments\"></shipmentIndex>" +
                              "</shipments>" +
                       "</orderData>" +
                      "</RootDTO>";

            XDocument doc = null;
            XDocument addedDocs = null;
            for (int count = 0; count < NUMBER_OF_XML; count++)
            {
                if (count == 0)
                {
                    doc = XDocument.Parse(xml);
                }
                else
                {
                    addedDocs = XDocument.Parse(xml);
                    XElement orderData = doc.Descendants("orderData").FirstOrDefault();
                    XElement addedOrderData = doc.Descendants("orderData").FirstOrDefault();
                    List<XElement> children = addedOrderData.Elements().ToList();
                    foreach (XElement element in children)
                    {
                        string tagName = element.Name.LocalName;
                        XElement lastElement = orderData.Elements(tagName).LastOrDefault();
                        if (lastElement == null)
                        {
                            orderData.Add(element);
                        }
                        else
                        {
                            lastElement.ReplaceWith(new XElement[] { lastElement, element});
                        }
                    }
                 }
            }

        }
    }
}