Created
December 14, 2010 12:48
-
-
Save wallymathieu/740363 to your computer and use it in GitHub Desktop.
In order to test if two XElement are equal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Something.utils | |
{ | |
public class XElementDiff | |
{ | |
public static void AssertEqual(XElement actual, XElement source) | |
{ | |
ValuesAreEqual(actual, source); | |
TagsAreEqual(actual, source); | |
var actualElements = actual.Elements(); | |
var sourceElements = source.Elements(); | |
if (actualElements.Count() != sourceElements.Count()) | |
throw new Exception("actualElements.Count() != sourceElements.Count()"); | |
for (int i = 0; i < actualElements.Count(); i++) | |
{ | |
AssertEqual(actualElements.ElementAt(i), sourceElements.ElementAt(i)); | |
} | |
} | |
private static void TagsAreEqual(XElement actual, XElement source) | |
{ | |
if (!actual.Name.LocalName.Equals(source.Name.LocalName) | |
&& !actual.Name.NamespaceName.Equals(source.Name.NamespaceName)) | |
throw new Exception(string.Format("{0}!={1}", actual.Name, source.Name)); | |
} | |
private static void ValuesAreEqual(XElement actual, XElement source) | |
{ | |
if (!actual.Value.Equals(source.Value)) | |
throw new Exception(string.Format("{0}!={1}", actual.Value, source.Value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment