Extract Numeric value from XmlNode - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Extract Numeric value from XmlNode

Demo Code


using System.Diagnostics;
using System.Xml;
using System.Collections.Generic;
using System;/*ww w. java2s  . co  m*/

public class Main{
        public static T ExtractNumeric<T>(XmlNode node, string nodeName, T defualtVal, bool isMust)
        {
            T 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
            {
                result = (T)Convert.ChangeType(nodeText, typeof(T));
            }

            return result;
        }
}

Related Tutorials