Casting XML element to boolean. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Casting XML element to boolean.

Demo Code


using System.Xml.Linq;
using System;/* w  w  w .  j av a2 s . com*/

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

Related Tutorials