Comparing XmlDocument for equality (content wise)

Blankman picture Blankman · May 27, 2010 · Viewed 12.2k times · Source

If I want to compare the contents of a XMlDocument, is it just like this?

XmlDocument doc1 = GetDoc1();
XmlDocument doc2 = GetDoc2();

if(doc1 == doc2)
{

}

I am not checking if they are both the same object reference, but if the CONTENTS of the xml are the same.

Answer

Max Toro picture Max Toro · May 27, 2010

Try the DeepEquals method on the XLinq API.

XDocument doc1 = GetDoc1(); 
XDocument doc2 = GetDoc2(); 
 
if(XNode.DeepEquals(doc1, doc2)) 
{ 
 
} 

See also Equality Semantics of LINQ to XML Trees