CSharp examples for System.Xml:XML Element
Retrieves the value of a integer attribute on an XML element.
using System.Xml.Linq; using System.Globalization; using System;/*from ww w .j a v a 2 s . co m*/ public class Main{ /// <summary> /// Retrieves the value of a integer attribute on an XML element. /// The attribute's value may be represented in either decimal or hexadecimal. /// An exception will be thrown if the attribute doesn't exist or is invalid. /// </summary> /// <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 integer value that was parsed.</returns> /// <exception cref="ArgumentException">Thrown if the attribute is missing.</exception> /// <exception cref="FormatException">Thrown if the attribute does not represent an integer.</exception> /// <seealso cref="ParseNumber"/> public static int GetIntegerAttribute (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."); int result; if ( ParseNumber(attribute.Value, out result) ) return result; throw new FormatException("A(n) \"" + element.Name + "\" element has an invalid \"" + name + "\" attribute: " + attribute.Value); } }