Retrieves the value of an enum-based attribute on an XML element, returning a default value if it is not found. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Retrieves the value of an enum-based attribute on an XML element, returning a default value if it is not found.

Demo Code


using System.Xml.Linq;
using System.Globalization;
using System;/*  w w w . j  a  v a2s .com*/

public class Main{
        /// <summary>
      /// Retrieves the value of an enum-based attribute on an XML element, returning a default value if it is not found.
      /// </summary>
      /// <typeparam name="EnumType">The type of the enum to search through.</typeparam>
      /// <param name="element">The XML element that holds the attribute.</param>
      /// <param name="name">The name of the attribute to get the value of.</param>
      /// <param name="defaultValue">The value to return if the attribute is not found.</param>
      /// <returns>The enum value corresponding to the attribute's value, or the default value if the attribute was not found.</returns>
      /// <exception cref="FormatException">Thrown if the attribute value does not correspond to a value in the enum.</exception>
      public static EnumType GetEnumAttribute<EnumType> (XElement element, string name, EnumType defaultValue)
      {
         XAttribute attribute = element.Attribute(name);
         if ( attribute == null )
            return defaultValue;

         EnumType result;
         if ( FindEnumValue<EnumType>(attribute.Value, out result) )
            return result;

         throw new FormatException("A(n) \"" + element.Name + "\" element has an invalid \"" + name + "\" attribute: " + attribute.Value);
      }
        /// <summary>
      /// Retrieves the value of an enum-based attribute on an XML element.
      /// An exception will be thrown if the attribute doesn't exist or is invalid.
      /// </summary>
      /// <typeparam name="EnumType">The type of the enum to search through.</typeparam>
      /// <param name="element">The XML element that holds the attribute.</param>
      /// <param name="name">The name of the attribute to get the value of.</param>
      /// <returns>The enum value corresponding to the attribute's value.</returns>
      /// <exception cref="FormatException">Thrown if the attribute value does not correspond to a value in the enum.</exception>
      public static EnumType GetEnumAttribute<EnumType> (XElement element, string name)
      {
         XAttribute attribute = element.Attribute(name);
         if ( attribute == null )
            throw new ArgumentException("A(n) \"" + element.Name + "\" element is missing the required \"" + name + "\" attribute.");

         EnumType result;
         if ( FindEnumValue<EnumType>(attribute.Value, out result) )
            return result;

         throw new FormatException("A(n) \"" + element.Name + "\" element has an invalid \"" + name + "\" attribute: " + attribute.Value);
      }
}

Related Tutorials