xml file comparison
I want to only compare the nodes of two xml files but not the 开发者_如何学Govalue of the nodes using c#. If the node format in two files are not same then it should pop-up a message..
I will use Linq to XML :
XDocument doc = XDocument.Parse(data);
var list = doc.DescendantNodes().Where(i => i is XElement);
and then use this to compare :
foreach (var item in list)
{
if (((XElement)item).Name.LocalName == propert.Name)
}
but your final implementation should check number of nodes and other issues
XDocument file1 = XDocument.Load("somefile1.xml");
XDocument file2 = XDocument.Load("somefile2.xml");
if (file1.Nodes().Intersect(file2.Nodes()).Count() > 0)
MessageBox.Show("hey i popped up");
Hope this helps...
Assuming that by "node format" you mean elements & their names, this will walk the element tree and compare the names:
void Main()
{
XElement thing = new XElement("test", new XElement("child") );
XElement otherThing = new XElement("test", new XElement("child") );
var comparer = new XElementComparer();
var areSame = comparer.Equals(thing, otherThing);
Console.WriteLine(areSame);
}
class XElementComparer : IEqualityComparer<XElement>
{
public bool Equals(XElement first, XElement second)
{
if (first.Name != second.Name)
return false;
else if (!first.Elements().SequenceEqual(second.Elements(), this))
return false;
else
return true;
}
public int GetHashCode(XElement element) { return element.GetHashCode(); }
}
精彩评论