XmlComparer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public XmlComparer (Flags flags, bool ignoreWS)
  19. {
  20. this.flags = flags;
  21. this.ignoreWS = ignoreWS;
  22. }
  23. public XmlComparer (Flags flags) : this (flags, true)
  24. {
  25. }
  26. public XmlComparer () : this (Flags.IgnoreAttribOrder)
  27. {
  28. }
  29. public bool AreEqualAttribs (XmlAttributeCollection attrs1, XmlAttributeCollection attrs2)
  30. {
  31. if (attrs1.Count != attrs2.Count)
  32. return false;
  33. for (int i=0; i<attrs1.Count; i++) {
  34. if ((flags & Flags.IgnoreAttribOrder) != 0) {
  35. string ln = attrs1[i].LocalName;
  36. string ns = attrs1[i].NamespaceURI;
  37. string val = attrs1[i].Value;
  38. XmlAttribute atr2 = attrs2[ln, ns];
  39. if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
  40. return false;
  41. } else {
  42. if (attrs1 [i].LocalName != attrs2 [i].LocalName)
  43. return false;
  44. if (attrs1 [i].NamespaceURI != attrs2 [i].NamespaceURI)
  45. return false;
  46. if (attrs1 [i].Value.Trim().ToLower() != attrs2 [i].Value.Trim().ToLower())
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. public bool AreEqualNodeList (XmlNodeList lst1, XmlNodeList lst2)
  53. {
  54. if (lst1.Count != lst2.Count)
  55. return false;
  56. for (int i=0; i<lst1.Count; i++) {
  57. if (!AreEqual (lst1[i], lst2[i]))
  58. return false;
  59. }
  60. return true;
  61. }
  62. public bool AreEqual (XmlNode node1, XmlNode node2)
  63. {
  64. lastCompare = node1.OuterXml + "\n" + node2.OuterXml;
  65. // skip XmlDeclaration
  66. if ((node1.NodeType == XmlNodeType.XmlDeclaration) &&
  67. (node2.NodeType == XmlNodeType.XmlDeclaration))
  68. return true;
  69. if (node1.NodeType != node2.NodeType)
  70. return false;
  71. if (node1.LocalName != node2.LocalName)
  72. return false;
  73. if (node1.NamespaceURI != node2.NamespaceURI)
  74. return false;
  75. if (node1.Attributes != null && node2.Attributes != null) {
  76. if (!AreEqualAttribs (node1.Attributes, node2.Attributes))
  77. return false;
  78. }
  79. else //one of nodes has no attrs
  80. if (node1.Attributes != null || node2.Attributes != null)
  81. return false;//and another has some
  82. if (!node1.HasChildNodes && !node2.HasChildNodes) {
  83. string val1 = node1.Value;
  84. string val2 = node2.Value;
  85. if (ignoreWS) //ignore white spaces
  86. {
  87. if (val1 != null)
  88. val1 = val1.Trim().Replace("\r\n", "\n");
  89. if (val2 != null)
  90. val2 = val2.Trim().Replace("\r\n", "\n");
  91. }
  92. return val1 == val2;
  93. }
  94. else {//one of nodes has some children
  95. if (!node1.HasChildNodes || !node2.HasChildNodes)
  96. return false;//and another has none
  97. return AreEqualNodeList (node1.ChildNodes, node2.ChildNodes);
  98. }
  99. }
  100. public string LastCompare
  101. {
  102. get {return lastCompare;}
  103. }
  104. }
  105. }