简体   繁体   中英

XML compare Elements and Values using LINQ to XML

    XElement client = new XElement("Root",
        new XElement("Child2", "2"),
        new XElement("Child1", "1"),
        new XElement("Child3", "hello world"),
        new XElement("Child5", "5")
        );

    XElement server = new XElement("Root",
        new XElement("Child1", "1"),
        new XElement("Child2", "2"),
        new XElement("Child3", "3"),
        new XElement("Child4", "4")
    );

What would be the right LINQ to XML to compare these 2 XMLs and get that the client has:
a) Element Child4 missing
b) Element Child5 is present but missing on the server
c) Element Child3 has different Value compare to the server one

Is there a simple way to get that those 3 "delta" nodes using LINQ to XML ??

====================================
What did I try so far:
1) XmlDiff

        var clientNode = XElement.Parse(client.ToString()).CreateReader();
        var serverNode = XElement.Parse(server.ToString()).CreateReader();
        var resultNode = new XDocument();
        var writer = resultNode.CreateWriter();

        var diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreDtd | XmlDiffOptions.IgnorePrefixes | XmlDiffOptions.IgnoreXmlDecl);
        diff.Compare(clientNode, serverNode, writer);
        writer.Flush();
        writer.Close();
        resultNode.ToString();

...but it gives me some complex XML that I would have to decode.

2) Converting it to string and comparing it

        string c = client.ToString().Trim();
        string s = server.ToString().Trim();
        c = c.Replace("Root", "").Replace("</", "").Replace("<", "").Replace(">", "");
        s = s.Replace("Root", "").Replace("</", "").Replace("<", "").Replace(">", "");

        List<string> c1 = c.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList<string>();
        List<string> s1 = s.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList<string>();

        List<string> delta1 = s1.Except(c1).ToList();
        List<string> delta2 = c1.Except(s1).ToList();

...but it gives me unreadable text back.

============================================
So my hope is that with LINQ to XML, we can quickly just get a list of the delta elements, maybe with a tag stating in what XML the returning element "x" was located.

Finding the element name differences is easy:

var clientElementNames = client.Elements().Select(x => x.Name);
var serverElementNames = server.Elements().Select(x => x.Name);

var clientOnly = clientElementNames.Except(serverElementNames);
var serverOnly = serverElementNames.Except(clientElementNames);

For elements with a different value, I'd probably do a join:

var differences = from clientElement in client.Elements()
                  join serverElement in server.Elements()
                    on clientElement.Name equals serverElement.Name
                  where clientElement.Value != serverElement.Value
                  select new { Name = clientElement.Name,
                               ClientValue = clientElement.Value,
                               ServerValue = serverElement.Value };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM