| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.Globalization;
- using System.Xml;
- /// <summary>
- /// Provides extension methods for the <see cref="XmlNode"/> class.
- /// </summary>
- public static class XmlNodeExtensions
- {
- /// <summary>
- /// Retrieves the value of the specified attribute from the <see cref="XmlNode"/>.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <param name="value">
- /// When this method returns, contains the value of the attribute, if found; otherwise, an empty string.
- /// </param>
- /// <returns><c>true</c> if the attribute is found and has a non-empty value; otherwise, <c>false</c>.</returns>
- private static bool GetAttributeValue(this XmlNode node, string attribute, out string value)
- {
- value = string.Empty;
- XmlAttribute attr = node.Attributes[attribute];
- if (attr is not null)
- {
- value = attr.Value;
- }
- return !string.IsNullOrEmpty(value);
- }
- /// <summary>
- /// Retrieves the string value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The string value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static string GetStringAttribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return value;
- }
- return default;
- }
- /// <summary>
- /// Retrieves the byte value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The byte value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static byte GetByteAttribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToByte(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the ushort value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The ushort value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static ushort GetUInt16Attribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToUInt16(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the short value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The short value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static short GetInt16Attribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToInt16(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the uint value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The uint value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static uint GetUInt32Attribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToUInt32(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the int value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The int value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static int GetInt32Attribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToInt32(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the float value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The float value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static float GetSingleAttribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToSingle(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the double value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The double value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static double GetDoubleAttribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToDouble(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the boolean value of the specified attribute.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <returns>
- /// The boolean value of the attribute, or the default value if the attribute is not found or is empty.
- /// </returns>
- public static bool GetBoolAttribute(this XmlNode node, string attribute)
- {
- if (node.GetAttributeValue(attribute, out string value))
- {
- return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
- }
- return default;
- }
- /// <summary>
- /// Retrieves the byte values from the specified delimited attribute and returns them in an array.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <param name="expectedCount">The expected number of byte values.</param>
- /// <returns>
- /// An array of byte values parsed from the attribute, or an array of default values if the attribute is not found
- /// or is empty.
- /// </returns>
- public static byte[] GetByteDelimitedAttribute(this XmlNode node, string attribute, int expectedCount)
- {
- byte[] result = new byte[expectedCount];
- if (node.GetAttributeValue(attribute, out string value))
- {
- string[] split = value.Split(',');
- for (int i = 0; i < expectedCount; ++i)
- {
- result[i] = Convert.ToByte(split[i], CultureInfo.InvariantCulture);
- }
- }
- return result;
- }
- /// <summary>
- /// Retrieves the signed byte values from the specified delimited attribute and returns them in an array.
- /// </summary>
- /// <param name="node">The XML node.</param>
- /// <param name="attribute">The name of the attribute.</param>
- /// <param name="expectedCount">The expected number of signed byte values.</param>
- /// <returns>
- /// An array of signed byte values parsed from the attribute, or an array of default values if the attribute is not found
- /// or is empty.
- /// </returns>
- public static sbyte[] GetSignedByteDelimitedAttribute(this XmlNode node, string attribute, int expectedCount)
- {
- sbyte[] result = new sbyte[expectedCount];
- if (node.GetAttributeValue(attribute, out string value))
- {
- string[] split = value.Split(',');
- for (int i = 0; i < expectedCount; ++i)
- {
- result[i] = Convert.ToSByte(split[i], CultureInfo.InvariantCulture);
- }
- }
- return result;
- }
- }
|