XMLWrapper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using System.Xml.XPath;
  6. using System.Text;
  7. using System.IO;
  8. using EasyDevCore.Common;
  9. #pragma warning disable CS8601 // Possible null reference assignment.
  10. #pragma warning disable CS8602 // Dereference of a possibly null reference.
  11. #pragma warning disable CS8603 // Possible null reference return.
  12. #pragma warning disable CS8604 // Possible null reference argument.
  13. #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
  14. namespace EasyDevCore.Common.Wrapper
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public class XMLWrapper: IDisposable
  20. {
  21. /// <summary>
  22. /// Gets the text.
  23. /// </summary>
  24. /// <value>
  25. /// The text.
  26. /// </value>
  27. public string Text { get; private set; }
  28. /// <summary>
  29. /// The m_ disposed
  30. /// </summary>
  31. private bool m_Disposed = false;
  32. /// <summary>
  33. /// The m_ reader
  34. /// </summary>
  35. private StringReader m_Reader = null;
  36. /// <summary>
  37. /// Gets the X doc.
  38. /// </summary>
  39. /// <value>
  40. /// The X doc.
  41. /// </value>
  42. public XDocument XDoc { get; private set; }
  43. /// <summary>
  44. /// Creates the intance.
  45. /// </summary>
  46. /// <param name="xml">The XML.</param>
  47. /// <returns></returns>
  48. public XMLWrapper CreateIntance(string xml)
  49. {
  50. return new XMLWrapper(xml);
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="XMLWrapper"/> class.
  54. /// </summary>
  55. /// <param name="xml">The XML.</param>
  56. public XMLWrapper(string xml)
  57. {
  58. Text = xml;
  59. m_Reader = new StringReader(xml);
  60. XDoc = XDocument.Load(m_Reader);
  61. }
  62. /// <summary>
  63. /// Gets the elements.
  64. /// </summary>
  65. /// <param name="xPathExpression">The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).</param>
  66. /// <param name="descendantElement">The descendant element.</param>
  67. /// <returns></returns>
  68. public IEnumerable<XElement> GetElements(string xPathExpression, XElement descendantElement = null)
  69. {
  70. if(descendantElement != null) descendantElement.XPathSelectElements(xPathExpression);
  71. return XDoc.XPathSelectElements(xPathExpression);
  72. }
  73. /// <summary>
  74. /// Gets the element.
  75. /// </summary>
  76. /// <param name="xPathExpression">The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).</param>
  77. /// <param name="descendantElement">The descendant element.</param>
  78. /// <returns></returns>
  79. public XElement GetElement(string xPathExpression, XElement descendantElement = null)
  80. {
  81. if (descendantElement != null) descendantElement.XPathSelectElement(xPathExpression);
  82. return XDoc.XPathSelectElement(xPathExpression);
  83. }
  84. /// <summary>
  85. /// Gets the element XML.
  86. /// </summary>
  87. /// <param name="xPathExpression">The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).</param>
  88. /// <param name="descendantElement">The descendant element.</param>
  89. /// <returns></returns>
  90. public IEnumerable<string> GetElementsXML(string xPathExpression, XElement descendantElement = null)
  91. {
  92. return GetElements(xPathExpression, descendantElement).Select(e => e.ToString());
  93. }
  94. /// <summary>
  95. /// Gets the element XML.
  96. /// </summary>
  97. /// <param name="xPathExpression">The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).</param>
  98. /// <param name="descendantElement">The descendant element.</param>
  99. /// <returns></returns>
  100. public string GetElementXML(string xPathExpression, XElement descendantElement = null)
  101. {
  102. return GetElement(xPathExpression, descendantElement).ToString();
  103. }
  104. /// <summary>
  105. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  106. /// </summary>
  107. public void Dispose()
  108. {
  109. Dispose(true);
  110. GC.SuppressFinalize(this);
  111. }
  112. /// <summary>
  113. /// Releases unmanaged and - optionally - managed resources.
  114. /// </summary>
  115. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  116. protected virtual void Dispose(bool disposing)
  117. {
  118. if (!this.m_Disposed)
  119. {
  120. if (disposing)
  121. {
  122. m_Reader.Close();
  123. m_Reader = null;
  124. XDoc = null;
  125. }
  126. m_Disposed = true;
  127. }
  128. }
  129. }
  130. }