Casting XML attribute to boolean. - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Casting XML attribute to boolean.

Demo Code


using System.Xml.Linq;
using System;/*from  w  w  w .  j a v  a  2 s  .c om*/

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

Related Tutorials