合并重复的XML父节点

本文关键字:XML 父节点 合并 | 更新日期: 2023-09-27 18:13:27

我在c#中使用StringBuilder创建了以下XML。我如何验证我的XML,如果我有相同的产品Id已经存在于我的XML中,那么不要创建新的行项目,只是添加现有的数量。

    <FL val="Product Details">
     <product no="1">
     <FL val="Product Id"><![CDATA[1991132000000252281]]></FL>
     <FL val="Product Description"><![CDATA[TEST]]></FL>
     <FL val="List Price"><![CDATA[10.00000]]></FL>
     <FL val="Quantity"><![CDATA[10.00000]]></FL>
     <FL val="Net Total"><![CDATA[100.0000000000]]></FL>
     </product>
     <product no="2">
     <FL val="Product Id"><![CDATA[1991132000000252281]]></FL>
     <FL val="Product Description"><![CDATA[TEST]]></FL>
     <FL val="List Price"><![CDATA[10.00000]]></FL>
     <FL val="Quantity"><![CDATA[5.00000]]></FL>
     <FL val="Net Total"><![CDATA[50.0000000000]]></FL>
     </product>
     <product no="3">
     <FL val="Product Id"><![CDATA[1991132000000252280]]></FL>
     <FL val="Product Description"><![CDATA[TEST2]]></FL>
     <FL val="List Price"><![CDATA[110.00000]]></FL>
     <FL val="Quantity"><![CDATA[5.00000]]></FL>
     <FL val="Net Total"><![CDATA[550.0000000000]]></FL>
     </product>
     <product no="4">
     <FL val="Product Id"><![CDATA[1991132000000252280]]></FL>
     <FL val="Product Description"><![CDATA[TEST2]]></FL>
     <FL val="List Price"><![CDATA[110.00000]]></FL>
     <FL val="Quantity"><![CDATA[5.00000]]></FL>
     <FL val="Net Total"><![CDATA[550.0000000000]]></FL>
     </product>

生成XML的示例代码

    public string GenerateXml(IEnumerable<BVSalesOrder> order)
    {
        var main = order.First();
        var xmlString = new StringBuilder();
        var rowCount = 1;
        int inc = 1;
        xmlString.Append("<SalesOrders>");
        xmlString.AppendFormat("<row no='"{0}'">", rowCount);
        xmlString.AppendFormat("<FL val='"Subject'"><![CDATA[{0}]]></FL>", main.order_no);
        xmlString.AppendFormat("<FL val='"Order Number'"><![CDATA[{0}]]></FL>", main.order_no);
        xmlString.AppendFormat("<FL val='"Account Name'"><![CDATA[{0}]]></FL>", main.cust_no);
        xmlString.AppendFormat("<FL val='"Product Details'">");
        foreach (var item in order)
        {
            if (item.EX_CHAR_KEY1 != "")
            {
                decimal unitPrice = (Convert.ToDecimal(item.BVUNITPRICE));
                decimal qty = (Convert.ToDecimal(item.BVORDQTY));
                decimal amount = (unitPrice * qty);

                xmlString.AppendFormat("<product no='"{0}'">", inc);
                xmlString.AppendFormat("<FL val='"Product Id'"><![CDATA[{0}]]></FL>", item.EX_CHAR_KEY1);
                xmlString.AppendFormat("<FL val='"Product Description'"><![CDATA[{0}]]></FL>", item.ORDD_DESCRIPTION);
                xmlString.AppendFormat("<FL val='"List Price'"><![CDATA[{0}]]></FL>", item.BVUNITPRICE);
                xmlString.AppendFormat("<FL val='"Quantity'"><![CDATA[{0}]]></FL>", item.BVORDQTY);
                xmlString.AppendFormat("<FL val='"Net Total'"><![CDATA[{0}]]></FL>", amount);
                xmlString.AppendFormat("<FL val='"Total'"><![CDATA[{0}]]></FL>", amount);
                xmlString.Append("</product>");
                inc++;
                if (inc > 100)
                    break;
            }
            if (item.EX_CHAR_KEY1 == "")
            {
                decimal unitPrice = (Convert.ToDecimal(item.BVUNITPRICE));
                decimal qty = (Convert.ToDecimal(item.BVORDQTY));
                decimal amount = (unitPrice * qty);
                xmlString.AppendFormat("<product no='"{0}'">", inc);
                xmlString.AppendFormat("<FL val='"Product Description'"><![CDATA[{0}]]></FL>", item.code + " (" + item.ORDD_DESCRIPTION + ")");
                xmlString.AppendFormat("<FL val='"Product Id'">1991132000000453001</FL>");
                xmlString.AppendFormat("<FL val='"List Price'"><![CDATA[{0}]]></FL>", item.BVUNITPRICE);
                xmlString.AppendFormat("<FL val='"Quantity'"><![CDATA[{0}]]></FL>", item.BVORDQTY);
                xmlString.AppendFormat("<FL val='"Net Total'"><![CDATA[{0}]]></FL>", amount);
                xmlString.AppendFormat("<FL val='"Total'"><![CDATA[{0}]]></FL>", amount);
                xmlString.Append("</product>");
                inc++;
                if (inc > 100)
                    break;
            }
        }
        xmlString.Append("</FL>");

        //close row
        xmlString.Append("</row>");
        rowCount++;
        xmlString.Append("</SalesOrders>");
        return xmlString.ToString();
    }
}

}

合并重复的XML父节点

您可以根据其键EX_CHAR_KEY1:

对订单产品进行分组
var orderProducts = order
    .GroupBy(p => p.EX_CHAR_KEY1, (id, products) => new
    {
        Id = id,
        Description = products.Select(p => p.ORDD_DESCRIPTION).First(),
        Price = products.Select(p => Convert.ToDecimal(p.BVUNITPRICE)).First(),
        Quantity = products.Select(p => Convert.ToDecimal(p.BVORDQTY)).Sum(),
    });
foreach (var item in orderProducts)
{
    //...
    var total = item.Price * item.Quantity;
    xmlString.AppendFormat("<product no='"{0}'">", inc);
    xmlString.AppendFormat("<FL val='"Product Id'"><![CDATA[{0}]]></FL>", item.Id);
    xmlString.AppendFormat("<FL val='"Product Description'"><![CDATA[{0}]]></FL>", item.Description);
    xmlString.AppendFormat("<FL val='"List Price'"><![CDATA[{0}]]></FL>", item.Price);
    xmlString.AppendFormat("<FL val='"Quantity'"><![CDATA[{0}]]></FL>", item.Quantity);
    xmlString.AppendFormat("<FL val='"Net Total'"><![CDATA[{0}]]></FL>", total);
    xmlString.AppendFormat("<FL val='"Total'"><![CDATA[{0}]]></FL>", total);
    // ...
}