XmlComparer.cs 3.2 KB

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