XmlNode.Extensions.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Globalization;
  3. using System.Xml;
  4. /// <summary>
  5. /// Provides extension methods for the <see cref="XmlNode"/> class.
  6. /// </summary>
  7. public static class XmlNodeExtensions
  8. {
  9. /// <summary>
  10. /// Retrieves the value of the specified attribute from the <see cref="XmlNode"/>.
  11. /// </summary>
  12. /// <param name="node">The XML node.</param>
  13. /// <param name="attribute">The name of the attribute.</param>
  14. /// <param name="value">
  15. /// When this method returns, contains the value of the attribute, if found; otherwise, an empty string.
  16. /// </param>
  17. /// <returns><c>true</c> if the attribute is found and has a non-empty value; otherwise, <c>false</c>.</returns>
  18. private static bool GetAttributeValue(this XmlNode node, string attribute, out string value)
  19. {
  20. value = string.Empty;
  21. XmlAttribute attr = node.Attributes[attribute];
  22. if (attr is not null)
  23. {
  24. value = attr.Value;
  25. }
  26. return !string.IsNullOrEmpty(value);
  27. }
  28. /// <summary>
  29. /// Retrieves the string value of the specified attribute.
  30. /// </summary>
  31. /// <param name="node">The XML node.</param>
  32. /// <param name="attribute">The name of the attribute.</param>
  33. /// <returns>
  34. /// The string value of the attribute, or the default value if the attribute is not found or is empty.
  35. /// </returns>
  36. public static string GetStringAttribute(this XmlNode node, string attribute)
  37. {
  38. if (node.GetAttributeValue(attribute, out string value))
  39. {
  40. return value;
  41. }
  42. return default;
  43. }
  44. /// <summary>
  45. /// Retrieves the byte value of the specified attribute.
  46. /// </summary>
  47. /// <param name="node">The XML node.</param>
  48. /// <param name="attribute">The name of the attribute.</param>
  49. /// <returns>
  50. /// The byte value of the attribute, or the default value if the attribute is not found or is empty.
  51. /// </returns>
  52. public static byte GetByteAttribute(this XmlNode node, string attribute)
  53. {
  54. if (node.GetAttributeValue(attribute, out string value))
  55. {
  56. return Convert.ToByte(value, CultureInfo.InvariantCulture);
  57. }
  58. return default;
  59. }
  60. /// <summary>
  61. /// Retrieves the ushort value of the specified attribute.
  62. /// </summary>
  63. /// <param name="node">The XML node.</param>
  64. /// <param name="attribute">The name of the attribute.</param>
  65. /// <returns>
  66. /// The ushort value of the attribute, or the default value if the attribute is not found or is empty.
  67. /// </returns>
  68. public static ushort GetUInt16Attribute(this XmlNode node, string attribute)
  69. {
  70. if (node.GetAttributeValue(attribute, out string value))
  71. {
  72. return Convert.ToUInt16(value, CultureInfo.InvariantCulture);
  73. }
  74. return default;
  75. }
  76. /// <summary>
  77. /// Retrieves the short value of the specified attribute.
  78. /// </summary>
  79. /// <param name="node">The XML node.</param>
  80. /// <param name="attribute">The name of the attribute.</param>
  81. /// <returns>
  82. /// The short value of the attribute, or the default value if the attribute is not found or is empty.
  83. /// </returns>
  84. public static short GetInt16Attribute(this XmlNode node, string attribute)
  85. {
  86. if (node.GetAttributeValue(attribute, out string value))
  87. {
  88. return Convert.ToInt16(value, CultureInfo.InvariantCulture);
  89. }
  90. return default;
  91. }
  92. /// <summary>
  93. /// Retrieves the uint value of the specified attribute.
  94. /// </summary>
  95. /// <param name="node">The XML node.</param>
  96. /// <param name="attribute">The name of the attribute.</param>
  97. /// <returns>
  98. /// The uint value of the attribute, or the default value if the attribute is not found or is empty.
  99. /// </returns>
  100. public static uint GetUInt32Attribute(this XmlNode node, string attribute)
  101. {
  102. if (node.GetAttributeValue(attribute, out string value))
  103. {
  104. return Convert.ToUInt32(value, CultureInfo.InvariantCulture);
  105. }
  106. return default;
  107. }
  108. /// <summary>
  109. /// Retrieves the int value of the specified attribute.
  110. /// </summary>
  111. /// <param name="node">The XML node.</param>
  112. /// <param name="attribute">The name of the attribute.</param>
  113. /// <returns>
  114. /// The int value of the attribute, or the default value if the attribute is not found or is empty.
  115. /// </returns>
  116. public static int GetInt32Attribute(this XmlNode node, string attribute)
  117. {
  118. if (node.GetAttributeValue(attribute, out string value))
  119. {
  120. return Convert.ToInt32(value, CultureInfo.InvariantCulture);
  121. }
  122. return default;
  123. }
  124. /// <summary>
  125. /// Retrieves the float value of the specified attribute.
  126. /// </summary>
  127. /// <param name="node">The XML node.</param>
  128. /// <param name="attribute">The name of the attribute.</param>
  129. /// <returns>
  130. /// The float value of the attribute, or the default value if the attribute is not found or is empty.
  131. /// </returns>
  132. public static float GetSingleAttribute(this XmlNode node, string attribute)
  133. {
  134. if (node.GetAttributeValue(attribute, out string value))
  135. {
  136. return Convert.ToSingle(value, CultureInfo.InvariantCulture);
  137. }
  138. return default;
  139. }
  140. /// <summary>
  141. /// Retrieves the double value of the specified attribute.
  142. /// </summary>
  143. /// <param name="node">The XML node.</param>
  144. /// <param name="attribute">The name of the attribute.</param>
  145. /// <returns>
  146. /// The double value of the attribute, or the default value if the attribute is not found or is empty.
  147. /// </returns>
  148. public static double GetDoubleAttribute(this XmlNode node, string attribute)
  149. {
  150. if (node.GetAttributeValue(attribute, out string value))
  151. {
  152. return Convert.ToDouble(value, CultureInfo.InvariantCulture);
  153. }
  154. return default;
  155. }
  156. /// <summary>
  157. /// Retrieves the boolean value of the specified attribute.
  158. /// </summary>
  159. /// <param name="node">The XML node.</param>
  160. /// <param name="attribute">The name of the attribute.</param>
  161. /// <returns>
  162. /// The boolean value of the attribute, or the default value if the attribute is not found or is empty.
  163. /// </returns>
  164. public static bool GetBoolAttribute(this XmlNode node, string attribute)
  165. {
  166. if (node.GetAttributeValue(attribute, out string value))
  167. {
  168. return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
  169. }
  170. return default;
  171. }
  172. /// <summary>
  173. /// Retrieves the byte values from the specified delimited attribute and returns them in an array.
  174. /// </summary>
  175. /// <param name="node">The XML node.</param>
  176. /// <param name="attribute">The name of the attribute.</param>
  177. /// <param name="expectedCount">The expected number of byte values.</param>
  178. /// <returns>
  179. /// An array of byte values parsed from the attribute, or an array of default values if the attribute is not found
  180. /// or is empty.
  181. /// </returns>
  182. public static byte[] GetByteDelimitedAttribute(this XmlNode node, string attribute, int expectedCount)
  183. {
  184. byte[] result = new byte[expectedCount];
  185. if (node.GetAttributeValue(attribute, out string value))
  186. {
  187. string[] split = value.Split(',');
  188. for (int i = 0; i < expectedCount; ++i)
  189. {
  190. result[i] = Convert.ToByte(split[i], CultureInfo.InvariantCulture);
  191. }
  192. }
  193. return result;
  194. }
  195. }