Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to compare if 2 XDocuments are equal ignoring element/attribute order?

Tags:

Unit testing my serialization code I found one failed because I had attributes listed in a different order (I'm just comparing the XDocument.ToString() values) and while I could fix that, it really doesn't matter to me in what order the elements or attributes appear as long as they're all there with the right name at the right level of hierarchy. I could probably write a method do this, but I'm wondering if there's an easy built in way I'm not aware of.

like image 439
Davy8 Avatar asked May 17 '09 22:05

Davy8


2 Answers

XNode has a DeepEquals function that should do the trick.

http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.deepequals.aspx

Update:

It appears that the DeepEquals function doesn't always work correctly. You may be best off implementing your own comparison routine.

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=400469

like image 190
Adam Ruth Avatar answered Sep 21 '22 13:09

Adam Ruth


Further to "Adam Ruth" I read through the microsoft link and this blog post is very useful..

Eric White: http://blogs.msdn.com/b/ericwhite/archive/2009/01/28/equality-semantics-of-linq-to-xml-trees.aspx

like image 41
Aim Kai Avatar answered Sep 20 '22 13:09

Aim Kai