Select XML Node Text - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Select XML Node Text

Demo Code


using System.Xml;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System;//from ww  w.  jav  a2s  .  c o  m

public class Main{
        public static string SelectNodeText(XmlNode node, string xPath)
        {
            if (node == null || string.IsNullOrEmpty(xPath))
                return string.Empty;

            XmlNode mNode = node.SelectSingleNode(xPath);
            return mNode == null ? string.Empty : mNode.InnerText;
        }
        public static string SelectNodeText(XmlDocument doc, string xPath)
        {
            if (doc == null || string.IsNullOrEmpty(xPath))
                return string.Empty;

            XmlNode node = doc.SelectSingleNode(xPath);
            return node == null ? string.Empty : node.InnerText;
        }
        #region ???InnerText
        public static string SelectNodeText(string xml, string xPath)
        {
            if (string.IsNullOrEmpty(xml) || string.IsNullOrEmpty(xPath))
                return string.Empty;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return SelectNodeText(doc, xPath);
        }
}

Related Tutorials