Extract Bool value from XmlNode - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Extract Bool value from XmlNode

Demo Code


using System.Diagnostics;
using System.Xml;
using System.Collections.Generic;
using System;/*from w  w w .jav a 2 s  . co  m*/

public class Main{
        public static bool ExtractBool (XmlNode node, string nodeName, bool defualtVal, bool isMust)
        {
            bool result = defualtVal;

            if (node == null || !node.HasChildNodes || node.SelectSingleNode(nodeName) == null)
            {
                if (isMust)
                {
                    Debug.Assert(false, "ExtactString Error!");
                }

                return result;
            }

            XmlNode childNode = node.SelectSingleNode(nodeName);

            string nodeText = childNode.InnerText;
            if (string.IsNullOrEmpty(nodeText))
            {
                if (isMust)
                {
                    Debug.Assert(false, "ExtactString Error!");
                }
            }
            else
            {
                if (nodeText.Trim().ToLower() == "true" || nodeText.Trim().ToLower() == "1")
                {
                    result = true;
                }

                if (nodeText.Trim().ToLower() == "false" || nodeText.Trim().ToLower() == "0")
                {
                    result = false;
                }
            }

            return result;
        }
}

Related Tutorials