首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >合并重复的XML父节点

合并重复的XML父节点
EN

Stack Overflow用户
提问于 2016-10-17 17:00:33
回答 1查看 75关注 0票数 0

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

代码语言:javascript
运行
复制
    <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的示例代码

代码语言:javascript
运行
复制
    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();
    }

}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-17 17:40:53

您可以按其键EX_CHAR_KEY1对订单产品进行分组。

代码语言:javascript
运行
复制
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);

    // ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40091891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档