XmlComparer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. namespace MonoTests.stand_alone.WebHarness
  5. {
  6. /// <summary>
  7. /// Summary description for XmlComparer.
  8. /// </summary>
  9. public class XmlComparer
  10. {
  11. [Flags]
  12. public enum Flags {
  13. IgnoreNone=0,
  14. IgnoreAttribOrder=1,
  15. IgnoreNodeOrder=2,
  16. }
  17. Flags flags;
  18. bool ignoreWS = true;
  19. string lastCompare = "";
  20. string _actual = "";
  21. string _expected = "";
  22. public XmlComparer (Flags flags, bool ignoreWS)
  23. {
  24. this.flags = flags;
  25. this.ignoreWS = ignoreWS;
  26. }
  27. public XmlComparer (Flags flags) : this (flags, true)
  28. {
  29. }
  30. public XmlComparer () : this (Flags.IgnoreAttribOrder)
  31. {
  32. }
  33. public bool AreEqualAttribs (XmlAttributeCollection expected, XmlAttributeCollection actual)
  34. {
  35. if (expected.Count != actual.Count)
  36. return false;
  37. for (int i=0; i<expected.Count; i++) {
  38. if ((flags & Flags.IgnoreAttribOrder) != 0) {
  39. string ln = expected[i].LocalName;
  40. string ns = expected[i].NamespaceURI;
  41. string val = expected[i].Value;
  42. _expected = ns+":"+ln+"="+val;
  43. XmlAttribute atr2 = actual[ln, ns];
  44. _actual = atr2 == null ? "<<missing>>" : ns + ":" + ln + "=" + atr2.Value;
  45. if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
  46. return false;
  47. } else {
  48. if (expected [i].LocalName != actual [i].LocalName)
  49. return false;
  50. if (expected [i].NamespaceURI != actual [i].NamespaceURI)
  51. return false;
  52. if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
  59. {
  60. if (expected.Count != actual.Count)
  61. return false;
  62. for (int i=0; i<expected.Count; i++) {
  63. if (!AreEqual (expected[i], actual[i]))
  64. return false;
  65. }
  66. return true;
  67. }
  68. public bool AreEqual (XmlNode expected, XmlNode actual)
  69. {
  70. lastCompare = expected.OuterXml + "\n" + actual.OuterXml;
  71. _actual = actual.OuterXml;
  72. _expected = expected.OuterXml;
  73. // skip XmlDeclaration
  74. if ((expected.NodeType == XmlNodeType.XmlDeclaration) &&
  75. (actual.NodeType == XmlNodeType.XmlDeclaration))
  76. return true;
  77. if (expected.NodeType != actual.NodeType)
  78. return false;
  79. if (expected.LocalName != actual.LocalName)
  80. return false;
  81. if (expected.NamespaceURI != actual.NamespaceURI)
  82. return false;
  83. if (expected.Attributes != null && actual.Attributes != null) {
  84. if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
  85. return false;
  86. _actual = actual.OuterXml;
  87. _expected = expected.OuterXml;
  88. }
  89. else //one of nodes has no attrs
  90. if (expected.Attributes != null || actual.Attributes != null)
  91. return false;//and another has some
  92. if (!expected.HasChildNodes && !actual.HasChildNodes) {
  93. string val1 = expected.Value;
  94. string val2 = actual.Value;
  95. if (ignoreWS) //ignore white spaces
  96. {
  97. if (val1 != null)
  98. val1 = val1.Trim().Replace("\r\n", "\n");
  99. if (val2 != null)
  100. val2 = val2.Trim().Replace("\r\n", "\n");
  101. }
  102. return val1 == val2;
  103. }
  104. else {//one of nodes has some children
  105. if (!expected.HasChildNodes || !actual.HasChildNodes)
  106. return false;//and another has none
  107. return AreEqualNodeList (expected.ChildNodes, actual.ChildNodes);
  108. }
  109. }
  110. public bool AreEqual (string expected, string actual)
  111. {
  112. XmlDocument eDocument = new XmlDocument();
  113. eDocument.LoadXml(expected);
  114. XmlNode eNode = eDocument.DocumentElement;
  115. XmlDocument aDocument = new XmlDocument ();
  116. aDocument.LoadXml (actual);
  117. XmlNode aNode = aDocument.DocumentElement;
  118. return AreEqual (eNode, aNode);
  119. }
  120. public string LastCompare
  121. {
  122. get {return lastCompare;}
  123. }
  124. public string Actual
  125. {
  126. get { return _actual; }
  127. }
  128. public string Expected
  129. {
  130. get { return _expected; }
  131. }
  132. }
  133. }