Select XML Attribute - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Select XML Attribute

Demo Code


using System.Xml;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System;//from  www  . j  av  a2 s . c  o  m

public class Main{
        public static string SelectAttribute(XmlNode node, string xPath)
        {
            if (node == null)
                return string.Empty;
            XmlNode mNode = node.SelectSingleNode(xPath); ;
            if (mNode != null)
                return mNode.Value;
            return string.Empty;
        }
        public static string SelectAttribute(XmlDocument doc, string xPath)
        {
            if (doc != null)
            {
                XmlNode node = doc.SelectSingleNode(xPath);
                if (node != null)
                    return node.Value;
            }
            return string.Empty;
        }
        #endregion ???InnerText

        #region ???Attribute
        public static string SelectAttribute(string xml, string xPath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            return SelectAttribute(doc, xPath);
        }
}

Related Tutorials