合并重复的XML父节点

时间:2016-10-17 17:00:34

标签: c# xml

我在c#中使用StringBuilder创建了以下XML。我如何验证我的XML,如果我的XML中已存在相同的产品ID,则不要创建新的订单项,只需在现有订单项中添加数量。

    <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();
    }

}

}

1 个答案:

答案 0 :(得分:1)

您可以按订单产品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);

    // ...
}