7 public class XmlComparer
17 string lastCompare = "";
19 string _expected = "";
21 public XmlComparer (Flags flags, bool ignoreWS)
24 this.ignoreWS = ignoreWS;
27 public XmlComparer (Flags flags) : this (flags, true)
31 public XmlComparer () : this (Flags.IgnoreAttribOrder)
35 public bool AreEqualAttribs (XmlAttributeCollection expected, XmlAttributeCollection actual)
37 if (expected.Count != actual.Count)
39 for (int i=0; i<expected.Count; i++) {
40 if ((flags & Flags.IgnoreAttribOrder) != 0) {
41 string ln = expected[i].LocalName;
42 string ns = expected[i].NamespaceURI;
43 string val = expected[i].Value;
44 _expected = ns+":"+ln+"="+val;
45 XmlAttribute atr2 = actual[ln, ns];
46 _actual = atr2 == null ? "<<missing>>" : ns + ":" + ln + "=" + atr2.Value;
47 if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
50 if (expected [i].LocalName != actual [i].LocalName)
52 if (expected [i].NamespaceURI != actual [i].NamespaceURI)
54 if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
61 public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
63 if (expected.Count != actual.Count)
65 for (int i=0; i<expected.Count; i++) {
66 if (!AreEqual (expected[i], actual[i]))
72 public bool AreEqual (XmlNode expected, XmlNode actual)
74 lastCompare = expected.OuterXml + "\n" + actual.OuterXml;
75 _actual = actual.OuterXml;
76 _expected = expected.OuterXml;
77 // skip XmlDeclaration
78 if ((expected.NodeType == XmlNodeType.XmlDeclaration) &&
79 (actual.NodeType == XmlNodeType.XmlDeclaration))
81 if (expected.NodeType != actual.NodeType)
83 if (expected.LocalName != actual.LocalName)
85 if (expected.NamespaceURI != actual.NamespaceURI)
87 if (expected.Attributes != null && actual.Attributes != null) {
88 if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
91 _actual = actual.OuterXml;
92 _expected = expected.OuterXml;
94 else //one of nodes has no attrs
95 if (expected.Attributes != null || actual.Attributes != null)
96 return false;//and another has some
97 if (!expected.HasChildNodes && !actual.HasChildNodes) {
98 string val1 = expected.Value;
99 string val2 = actual.Value;
100 if (ignoreWS) //ignore white spaces
103 val1 = val1.Trim().Replace("\r\n", "\n");
105 val2 = val2.Trim().Replace("\r\n", "\n");
109 else {//one of nodes has some children
110 if (!expected.HasChildNodes || !actual.HasChildNodes)
111 return false;//and another has none
112 return AreEqualNodeList (expected.ChildNodes, actual.ChildNodes);
116 public string LastCompare
118 get {return lastCompare;}
123 get { return _actual; }
126 public string Expected
128 get { return _expected; }
131 public static void AssertAreEqual (string expected, string actual) {
132 AssertAreEqual (expected, actual, String.Empty);
135 public static void AssertAreEqual (string expected, string actual, string msg) {
138 XmlDocument or = new XmlDocument ();
139 or.LoadXml (expected);
140 XmlDocument dr = new XmlDocument ();
142 XmlComparer comparer = new XmlComparer ();
143 if (!comparer.AreEqual (or, dr))
144 Assert.AreEqual (comparer.Expected, comparer.Actual, msg);
146 catch (AssertionException) {
149 catch (Exception e) {
150 //swallow e when there is XML error and fallback
151 //to the text comparison
152 Assert.AreEqual (expected, actual, msg);