Xml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Xml;
  6. using System.Security;
  7. namespace FastReport.Messaging.Xmpp
  8. {
  9. /// <summary>
  10. /// Represents a static class to simplify the work with XmlElement instance.
  11. /// </summary>
  12. internal static class Xml
  13. {
  14. #region Public Methods
  15. /// <summary>
  16. /// Creates a new XmlElement instance.
  17. /// </summary>
  18. /// <param name="name">The name of the element.</param>
  19. /// <param name="nspace">The namespace of the element.</param>
  20. /// <returns>A new instance of the <see cref="XmlElement"/> class.</returns>
  21. public static XmlElement CreateElement(string name, string nspace)
  22. {
  23. return new XmlDocument().CreateElement(name, nspace);
  24. }
  25. /// <summary>
  26. /// Adds the specified child to the end of child nodes of element.
  27. /// </summary>
  28. /// <param name="element">The element for add the child to.</param>
  29. /// <param name="child">The child node to add.</param>
  30. /// <returns>A XmlElement instance.</returns>
  31. public static XmlElement AddChild(XmlElement element, XmlElement child)
  32. {
  33. XmlNode node = element.OwnerDocument.ImportNode(child, true);
  34. element.AppendChild(node);
  35. return element;
  36. }
  37. /// <summary>
  38. /// Adds the attribute to XmlElement with spefied name and value.
  39. /// </summary>
  40. /// <param name="element">The element for add the attribute to.</param>
  41. /// <param name="name">The name of attribute.</param>
  42. /// <param name="value">The value of attribute.</param>
  43. /// <returns>A XmlElement instance.</returns>
  44. public static XmlElement AddAttribute(XmlElement element, string name, string value)
  45. {
  46. element.SetAttribute(name, value);
  47. return element;
  48. }
  49. /// <summary>
  50. /// Adds the specified text to the end of child nodes of element.
  51. /// </summary>
  52. /// <param name="element">The element for add the text to.</param>
  53. /// <param name="text">The text for add.</param>
  54. /// <returns>A XmlElement instance.</returns>
  55. public static XmlElement AddText(XmlElement element, string text)
  56. {
  57. element.AppendChild(element.OwnerDocument.CreateTextNode(text));
  58. return element;
  59. }
  60. /// <summary>
  61. /// Converts the XmlElement instance to a string.
  62. /// </summary>
  63. /// <param name="element">The element to convert to.</param>
  64. /// <param name="includeDeclaration">True if needed to include XML declaration.</param>
  65. /// <param name="leaveOpen">True if needed to leave the tag of an empty element open.</param>
  66. /// <returns>The XmlElement instance as string.</returns>
  67. public static string ToXmlString(XmlElement element, bool includeDeclaration, bool leaveOpen)
  68. {
  69. StringBuilder sb = new StringBuilder("<" + element.Name);
  70. if (!String.IsNullOrEmpty(element.NamespaceURI))
  71. {
  72. sb.Append(" xmlns='" + element.NamespaceURI + "'");
  73. }
  74. foreach (XmlAttribute attribute in element.Attributes)
  75. {
  76. if (attribute.Name != "xmlns" && attribute.Value != null)
  77. {
  78. sb.Append(" " + attribute.Name + "='" + SecurityElement.Escape(attribute.Value.ToString()) + "'");
  79. }
  80. }
  81. if (element.IsEmpty)
  82. {
  83. sb.Append("/>");
  84. }
  85. else
  86. {
  87. sb.Append(">");
  88. foreach (XmlNode child in element.ChildNodes)
  89. {
  90. if (child is XmlElement)
  91. {
  92. sb.Append(Xml.ToXmlString(child as XmlElement, false, false));
  93. }
  94. else if (child is XmlText)
  95. {
  96. sb.Append((child as XmlText).InnerText);
  97. }
  98. }
  99. sb.Append("</" + element.Name + ">");
  100. }
  101. string xml = "";
  102. if (includeDeclaration)
  103. {
  104. //xml = "<?xml version='1.0' encoding='UTF-8'?>";
  105. xml = "<?xml version='1.0'?>";
  106. }
  107. xml += sb.ToString();
  108. if (leaveOpen)
  109. {
  110. xml = Regex.Replace(xml, "/>$", ">");
  111. }
  112. return xml;
  113. }
  114. #endregion // Public Methods
  115. }
  116. }