将多个 XML 文件合并为一个 XML

本文关键字:XML 一个 合并 文件 | 更新日期: 2023-09-27 18:33:30

我有一堆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 组合后的结果.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 字符串

将多个 XML 文件合并为一个 XML

为了简化示例代码,我将相同的 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});
                        }
                    }
                 }
            }
        }
    }
}