XmlUtils.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Xml;
  30. namespace GHTUtils.Xml
  31. {
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public class XmlUtils
  36. {
  37. public XmlUtils()
  38. {
  39. }
  40. /// <summary>
  41. /// lecsicographic sorting of the XmlNodes child elements and attributes.
  42. /// </summary>
  43. /// <param name="a_xmlString">Xml string to sort.</param>
  44. /// <returns></returns>
  45. public static string SortXml(string a_xmlString)
  46. {
  47. XmlDocument l_toSort = new XmlDocument();
  48. l_toSort.LoadXml(a_xmlString);
  49. SortXml(l_toSort.DocumentElement);
  50. return l_toSort.OuterXml;
  51. }
  52. /// <summary>
  53. /// Inplace pre-order recursive lecsicographic sorting of the XmlNodes child elements and attributes.
  54. /// </summary>
  55. /// <param name="a_root">The root to be sorted.</param>
  56. public static void SortXml(XmlNode a_root)
  57. {
  58. SortAttributes(a_root.Attributes);
  59. SortElements(a_root);
  60. foreach (XmlNode l_currentChild in a_root.ChildNodes)
  61. {
  62. SortXml(l_currentChild);
  63. }
  64. }
  65. /// <summary>
  66. /// Sorts an attributes collection alphabeticlly.
  67. /// Uses bubble sort.
  68. /// </summary>
  69. /// <param name="a_toSort">The attribute collection to sort.</param>
  70. public static void SortAttributes(XmlAttributeCollection a_toSort)
  71. {
  72. if (a_toSort == null)
  73. {
  74. return;
  75. }
  76. bool l_change = true;
  77. while (l_change)
  78. {
  79. l_change = false;
  80. for (int i=1; i<a_toSort.Count; i++)
  81. {
  82. if (String.Compare(a_toSort[i].Name, a_toSort[i-1].Name, true) < 0)
  83. {
  84. //Replace
  85. a_toSort.InsertBefore(a_toSort[i], a_toSort[i-1]);
  86. l_change = true;
  87. }
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Sorts a XmlNodeList alphbeticlly, by the names of the elements.
  93. /// Uses bubble sort.
  94. /// </summary>
  95. /// <param name="a_toSort">The node list to sort.</param>
  96. public static void SortElements(XmlNode a_toSort)
  97. {
  98. bool l_change = true;
  99. while (l_change)
  100. {
  101. l_change = false;
  102. for (int i=1; i<a_toSort.ChildNodes.Count; i++)
  103. {
  104. if ( String.Compare(a_toSort.ChildNodes[i].Name, a_toSort.ChildNodes[i-1].Name, true) < 0)
  105. {
  106. //Replace:
  107. a_toSort.InsertBefore(a_toSort.ChildNodes[i], a_toSort.ChildNodes[i-1]);
  108. l_change = true;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }