views:

93

answers:

1

My first XElement is:

XElement sourceFile = new XElement("source",
                from o in Version1.Element("folder").Elements("folders").ElementAt(0).Elements("folder")
                where o.Name != null && o.Name == "folder"
                select new XElement("data",
                    new XElement("name",(string) o.Attribute("name")),
                    new XElement("filesCount", (string)o.Attribute("folderCount")),
                    new XElement("filesCount", (string)o.Attribute("filesCount"))
            )); 
            //,o)

My second is:

XElement targetFile = new XElement("target",
            from o in Version2.Element("folder").Elements("folders").ElementAt(0).Elements("folder")
            where o.Name != null && o.Name == "folder"
            select new XElement("data",
                new XElement("name", (string)o.Attribute("name")),
                new XElement("filesCount", (string)o.Attribute("folderCount")),
                new XElement("filesCount", (string)o.Attribute("filesCount"))
        )); 

I'd like to find the delta (the source always contains the target) something like this... sadly my is not working:

XElement nodenotinsource = new XElement ("result",
            from y in sourceFile.Elements().Except(from o in targetFile.Elements())
           select new XElement("ttt", y.Element("name").Value));

Version1 and Version2 were created like this:

XElement Version1 = XElement.Load(@"C:\output\xmltestO.xml");
XElement Version2 = XElement.Load(@"C:\output\xmltestO.xml");

where the two files are the same except the change the program should find...

A: 

(In the code in your question, you are loading the same file into Version1 and Version2. I’ll assume this is a typo and you are actually loading different files.)

You can’t use Except to compare XElements. You are creating separate instances of XElement. Even though they contain the same contents, they will not compare as equal.

Therefore, you need to compare the original data. For example:

var sourceData =
    from o in Version1.Element("folder").Elements("folders").ElementAt(0).Elements("folder")
    where o.Name != null && o.Name == "folder"
    select new {
        Name = (string) o.Attribute("name")),
        FolderCount = (string)o.Attribute("folderCount")),
        FilesCount = (string)o.Attribute("filesCount"))
    };

Then do the same with the target file to get targetData. Finally, you can compare them using Except and then generate your final XElement:

XElement nodenotinsource = new XElement ("result",
    from y in sourceData.Except(targetData)
    select new XElement("ttt", y.Name));
Timwi