Casting XML element to int32. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Casting XML element to int32.

Demo Code


using System.Xml.Linq;
using System;//from   w w w. ja va2 s  . c  o  m

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

Related Tutorials