Get XML Child Value - CSharp System.Xml

CSharp examples for System.Xml:XML Child

Description

Get XML Child Value

Demo Code


using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.IO;/*from   w w w  .  ja v  a 2s  .  c  om*/
using System;

public class Main{
        public static string GetChildValue(XmlNode parentNode, string childName, string defaultValue)
        {
            var node = parentNode.SelectSingleNode(childName);
            if (node != null)
                return node.InnerText;
            else
                return defaultValue;
        }
        public static Guid GetChildValue(XmlNode parentNode, string childName, Guid defaultValue)
        {
            var node = parentNode.SelectSingleNode(childName);
            if (node != null)
                return new Guid(node.InnerText);
            else
                return defaultValue;
        }
        public static bool GetChildValue(XmlNode parentNode, string childName, bool defaultValue)
        {
            var node = parentNode.SelectSingleNode(childName);
            if (node != null)
                return bool.Parse(node.InnerText);
            else
                return defaultValue;
        }
        public static int GetChildValue(XmlNode parentNode, string childName, int defaultValue)
        {
            var node = parentNode.SelectSingleNode(childName);
            if (node != null)
                return int.Parse(node.InnerText);
            else
                return defaultValue;
        }
        public static double GetChildValue(XmlNode parentNode, string childName, double defaultValue)
        {
            var node = parentNode.SelectSingleNode(childName);
            if (node != null)
                return double.Parse(node.InnerText);
            else
                return defaultValue;
        }
        #endregion

        #region GetChildValue

        public static string GetChildValue(XmlNode parentNode, string childName)
        {
            return GetChildValue(parentNode, childName, null);
        }
}

Related Tutorials