我有XML示例
<order>
<orderItem>
<OrderId>Ex1</OrderId>
<amount>100</amount>
</orderItem>
<orderItem>
<OrderId>Ex2</OrderId>
<amount>150</amount>
</orderItem>
<orderItem>
<OrderId>Ex1</OrderId>
<amount>250</amount>
</orderItem>
</order>
如果OrderId标记值等于另一个OrderId标记值,则添加Amount标记并删除新添加的标记。
我需要的结果输出应该如下
<order>
<orderItem>
<OrderId>Ex1</OrderId>
<amount>350</amount>
</orderItem>
<orderItem>
<OrderId>Ex2</OrderId>
<amount>150</amount>
</orderItem>
</order>
OrderId值应该更改。
string filename = "File Location";
var doc = XDocument.Load(filename);
var results = doc.Descendants("order");
var orderItemcount = results.Descendants("orderItem").Count();
我不知道下一步该做什么。
先谢谢。
更新:
我喜欢下面的新XML
<order>
<orderItem>
<Orders>
<OrderId>Ex1</OrderId>
<amount>100</amount>
</Orders>
</orderItem>
<orderItem>
<Orders>
<OrderId>Ex2</OrderId>
<amount>150</amount>
</Orders>
</orderItem>
<orderItem>
<Orders>
<OrderId>Ex1</OrderId>
<amount>250</amount>
</Orders>
</orderItem>
</order>
如果OrderId为Same,则将该OrderItem标记添加到匹配标记。 结果我需要如下所示
<order>
<orderItem>
<Orders>
<OrderId>Ex1</OrderId>
<amount>100</amount>
</Orders>
<Orders>
<OrderId>Ex1</OrderId>
<amount>250</amount>
</orderItem>
<orderItem>
<Orders>
<OrderId>Ex2</OrderId>
<amount>150</amount>
<Orders>
</orderItem>
</order>
更新答案
var groups = doc.Descendants("order").Descendants("orderItem").Descendants("Orders")
.GroupBy(x => (string)x.Element("OrderId"))
.ToList();
XElement newXml = new XElement("order");
foreach (var group in groups)
{
newXml.Add(new XElement("OrderList", new object[] {
// new XAttribute("OrderId", group.Key), // If you Need Key for Attribute add this.
group
}));
}
答案 0 :(得分:1)
有几个步骤:
代码非常简单:
while (true){
//hide the paragraph
$("para").hide()
//show the paragraph slowly
$("para").show("slow")
}
有关正常工作的演示,请参阅this fiddle。
答案 1 :(得分:1)
通过UIView.init()
转换为OrderId
的标准组,可以通过以下LINQ to XML查询来完成:
Sum(amount)
答案 2 :(得分:0)
使用xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var groups = doc.Descendants("orderItem")
.GroupBy(x => (string)x.Element("OrderId"))
.Select(x => x.Select(y => new {orderId = y, amount = (int)y.Element("amount")}))
.ToList();
for (int groupNum = groups.Count - 1; groupNum >= 0; groupNum--)
{
var group = groups[groupNum];
int total = group.Select(x => x.amount).Sum();
for (int orderItemCount = group.Count() - 1; orderItemCount >= 0; orderItemCount--)
{
var orderItem = group.Skip(orderItemCount).FirstOrDefault();
if (orderItemCount > 0)
{
orderItem.orderId.Remove();
}
else
{
XElement amount = orderItem.orderId.Element("amount");
amount.Value = total.ToString();
}
}
}
}
}
}