【发布时间】:2016-02-07 12:49:57
【问题描述】:
我有一堆 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.xml和2.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);
}
我想要实现的是附加元素 - “items”和“shipments” 到 1 个 XML 中,代表一个订单。
我正在使用此代码让我的 XML 字符串:var xml = XElement.Parse(renderedOutput); 代表我的 XML 字符串
【问题讨论】: