开发者

how to compare xml attributes with linq to XML?

开发者 https://www.devze.com 2023-03-30 22:07 出处:网络
I have an XML that is something like this. <root> <element attributeId=\"1\" attribute2=\"1\" attribute3=\"A\" />

I have an XML that is something like this.

<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>

I want to compare the attributes that changed for an element with the same attributeId, I created a class to store the results that is something like this:

public class Result
{
    public string AttributeName { get; set; }
    public string OldValue { get; set; }
    public string NewValue { get; set; }
}

I want to get from that a list of Results, I created a variable:

List<Result> result;

if I run the next code:

foreach (Result item in result)
{
    /* Write results */
}

The expected result would be:

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)

This has to be accomplished with linq to xml and I already know about the XmlDiff from microsoft but this tool is not an option for me. I have over a week researching and trying to get this solved. Ideally the number of attributes is going to change and the name of them as well so if some one can find out a way to do the same above for an XML like

<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开发者_Python百科" aa="1" bb="B" cc="X" />
</root>


I think its not possible using Linq2xml. You may simply iterate xml elements using XmlReader and manually fill your List<Result>.

0

精彩评论

暂无评论...
验证码 换一张
取 消