xml element comparison
Wow to compare the elements from two different xml files in C#? Onl开发者_开发百科y element name has to be compared not the element value.
I am using:
XDocument file1 = XDocument.Load(dest_filename);
XDocument file2 = XDocument.Load(source_filename);
if (file1.Nodes().Intersect(file2.Nodes()).Count() > 0)
{
MessageBox.Show("hey i popped up");
}
but it is comparing the value too, which i don't want to compare..
Given file1.xml:
<root>
<a></a>
<b></b>
<c></c>
</root>
and file2.xml:
<root>
<a></a>
<b></b>
<b></b>
<c></c>
<d></d>
</root>
The following code will result in four message boxes (for nodes a, b, b and c)
XDocument doc = XDocument.Load(System.IO.Path.GetFullPath("file1.xml"));
XDocument doc2 = XDocument.Load(System.IO.Path.GetFullPath("file2.xml"));
var matches = from a in doc.Element("root").Descendants()
join b in doc2.Element("root").Descendants() on a.Name equals b.Name
select new { First = a, Second = b };
foreach (var n in matches)
MessageBox.Show(n.First.ToString() + " matches " + n.Second.ToString());
Hope that helps :)
精彩评论