XmlComparer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 expected, XmlAttributeCollection actual)
  32. {
  33. if (expected.Count != actual.Count)
  34. return false;
  35. for (int i=0; i<expected.Count; i++) {
  36. if ((flags & Flags.IgnoreAttribOrder) != 0) {
  37. string ln = expected[i].LocalName;
  38. string ns = expected[i].NamespaceURI;
  39. string val = expected[i].Value;
  40. _expected = ns+":"+ln+"="+val;
  41. XmlAttribute atr2 = actual[ln, ns];
  42. _actual = atr2 == null ? "<<missing>>" : ns + ":" + ln + "=" + atr2.Value;
  43. if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
  44. return false;
  45. } else {
  46. if (expected [i].LocalName != actual [i].LocalName)
  47. return false;
  48. if (expected [i].NamespaceURI != actual [i].NamespaceURI)
  49. return false;
  50. if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
  57. {
  58. if (expected.Count != actual.Count)
  59. return false;
  60. for (int i=0; i<expected.Count; i++) {
  61. if (!AreEqual (expected[i], actual[i]))
  62. return false;
  63. }
  64. return true;
  65. }
  66. public bool AreEqual (XmlNode expected, XmlNode actual)
  67. {
  68. lastCompare = expected.OuterXml + "\n" + actual.OuterXml;
  69. _actual = actual.OuterXml;
  70. _expected = expected.OuterXml;
  71. // skip XmlDeclaration
  72. if ((expected.NodeType == XmlNodeType.XmlDeclaration) &&
  73. (actual.NodeType == XmlNodeType.XmlDeclaration))
  74. return true;
  75. if (expected.NodeType != actual.NodeType)
  76. return false;
  77. if (expected.LocalName != actual.LocalName)
  78. return false;
  79. if (expected.NamespaceURI != actual.NamespaceURI)
  80. return false;
  81. if (expected.Attributes != null && actual.Attributes != null) {
  82. if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
  83. return false;
  84. _actual = actual.OuterXml;
  85. _expected = expected.OuterXml;
  86. }
  87. else //one of nodes has no attrs
  88. if (expected.Attributes != null || actual.Attributes != null)
  89. return false;//and another has some
  90. if (!expected.HasChildNodes && !actual.HasChildNodes) {
  91. string val1 = expected.Value;
  92. string val2 = actual.Value;
  93. if (ignoreWS) //ignore white spaces
  94. {
  95. if (val1 != null)
  96. val1 = val1.Trim().Replace("\r\n", "\n");
  97. if (val2 != null)
  98. val2 = val2.Trim().Replace("\r\n", "\n");
  99. }
  100. return val1 == val2;
  101. }
  102. else {//one of nodes has some children
  103. if (!expected.HasChildNodes || !actual.HasChildNodes)
  104. return false;//and another has none
  105. return AreEqualNodeList (expected.ChildNodes, actual.ChildNodes);
  106. }
  107. }
  108. public string LastCompare
  109. {
  110. get {return lastCompare;}
  111. }
  112. public string Actual
  113. {
  114. get { return _actual; }
  115. }
  116. public string Expected
  117. {
  118. get { return _expected; }
  119. }
  120. }
  121. }