Casting XML attribute to int32. - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Casting XML attribute to int32.

Demo Code


using System.Xml.Linq;
using System;//from   w ww  .  j  a  v  a2  s .  co  m

public class Main{
        /// <summary>
        /// Casting attribute to int32.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Int32? CastAttributeToInt32(this XElement element, String key)
        {
            Int32 x;
            if (element.Attribute(key) == null) { return null; }
            if (Int32.TryParse(element.Attribute(key).Value, out x))
            {
                return x;
            }
            return null;
        }
}

Related Tutorials