using System;
using System.Globalization;
using System.Xml;
///
/// Provides extension methods for the class.
///
public static class XmlNodeExtensions
{
///
/// Retrieves the value of the specified attribute from the .
///
/// The XML node.
/// The name of the attribute.
///
/// When this method returns, contains the value of the attribute, if found; otherwise, an empty string.
///
/// true if the attribute is found and has a non-empty value; otherwise, false.
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);
}
///
/// Retrieves the string value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The string value of the attribute, or the default value if the attribute is not found or is empty.
///
public static string GetStringAttribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return value;
}
return default;
}
///
/// Retrieves the byte value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The byte value of the attribute, or the default value if the attribute is not found or is empty.
///
public static byte GetByteAttribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToByte(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the ushort value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The ushort value of the attribute, or the default value if the attribute is not found or is empty.
///
public static ushort GetUInt16Attribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToUInt16(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the short value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The short value of the attribute, or the default value if the attribute is not found or is empty.
///
public static short GetInt16Attribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToInt16(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the uint value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The uint value of the attribute, or the default value if the attribute is not found or is empty.
///
public static uint GetUInt32Attribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToUInt32(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the int value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The int value of the attribute, or the default value if the attribute is not found or is empty.
///
public static int GetInt32Attribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToInt32(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the float value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The float value of the attribute, or the default value if the attribute is not found or is empty.
///
public static float GetSingleAttribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToSingle(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the double value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The double value of the attribute, or the default value if the attribute is not found or is empty.
///
public static double GetDoubleAttribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToDouble(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the boolean value of the specified attribute.
///
/// The XML node.
/// The name of the attribute.
///
/// The boolean value of the attribute, or the default value if the attribute is not found or is empty.
///
public static bool GetBoolAttribute(this XmlNode node, string attribute)
{
if (node.GetAttributeValue(attribute, out string value))
{
return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
}
return default;
}
///
/// Retrieves the byte values from the specified delimited attribute and returns them in an array.
///
/// The XML node.
/// The name of the attribute.
/// The expected number of byte values.
///
/// An array of byte values parsed from the attribute, or an array of default values if the attribute is not found
/// or is empty.
///
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;
}
///
/// Retrieves the signed byte values from the specified delimited attribute and returns them in an array.
///
/// The XML node.
/// The name of the attribute.
/// The expected number of signed byte values.
///
/// 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.
///
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;
}
}