【发布时间】:2011-08-26 23:11:43
【问题描述】:
我有一个类似这样的 XML。
<root>
<element attributeId="1" attribute2="1" attribute3="A" />
<element attributeId="1" attribute2="2" attribute3="A" />
<element attributeId="2" attribute2="1" attribute3="A" />
<element attributeId="3" attribute2="1" attribute3="A" />
<element attributeId="3" attribute2="1" attribute3="B" />
</root>
我想比较具有相同attributeId的元素更改的属性,我创建了一个类来存储类似这样的结果:
public class Result
{
public string AttributeName { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
}
我想从中获取结果列表,我创建了一个变量:
List<Result> result;
如果我运行下一个代码:
foreach (Result item in result)
{
/* Write results */
}
预期的结果是:
Attribute2 | 1 | 2 (For ID 1)
Attribute2 | 1 | Null (For ID 2 as there is no value to compare)
Attribute3 | A | B (For ID 3)
这必须通过 linq to xml 来完成,我已经知道来自 microsoft 的 XmlDiff,但是这个工具对我来说不是一个选项。我有一个多星期的时间研究并试图解决这个问题。理想情况下,属性的数量和它们的名称也会发生变化,所以如果有人能找到一种方法来对像
这样的 XML 做同样的事情<root>
<element attid="1" aa="1" bb="A" cc="X" />
<element attid="1" aa="2" bb="A" cc="Y" />
<element attid="2" aa="1" bb="A" cc="X" />
<element attid="3" aa="1" bb="A" cc="X" />
<element attid="3" aa="1" bb="B" cc="X" />
</root>
【问题讨论】:
-
你比较的是什么属性?我看到“新”和“旧”,但与什么相比?您要比较哪些元素的哪些属性?你在这里说的没有任何意义。
-
抱歉混淆了,基本上我想比较按属性 ID 分组的同名属性,并获得有差异的属性,在同一个第一个示例中,我最多可以获得 2 条记录ID,每个不同的属性一个。如果没有其他 ID 可比较,我应该将这两个属性作为记录。
标签: c# asp.net linq-to-xml