Gets the value from XML attribute at the specified node. - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Gets the value from XML attribute at the specified node.

Demo Code

// All rights reserved.
using System.Diagnostics;
using System.Collections.Specialized;
using System.Xml;
using System.Data;
using System;//from ww  w. j  a va  2 s  . co m

public class Main{
        public static string GetAttributeValue(XmlNode node, string attributeName)
        {
            string value = null;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    value = attribute.Value;
                }
            }
            return value;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value from an attribute at the specified node.
        /// </summary>
        /// <param name="node">The XmlNode from which this method will get the value of an attribute.</param>
        /// <param name="attributeName">Name of the attribute that will be read.</param>
        /// <param name="value">Attribute value read by this method</param>
        /// <returns>True if success.</returns>
        /// -----------------------------------------------------------------------------
        public static bool GetAttributeValue(XmlNode node, string attributeName, ref UInt32 value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    string strValue = attribute.Value;
                    value = UInt32.Parse(strValue);
                    success = true;
                }
            }
            return success;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value from an attribute at the specified node.
        /// </summary>
        /// <param name="node">The XmlNode from which this method will get the value of an attribute.</param>
        /// <param name="attributeName">Name of the attribute that will be read.</param>
        /// <param name="value">Attribute value read by this method</param>
        /// <returns>True if success.</returns>
        /// -----------------------------------------------------------------------------
        public static bool GetAttributeValue(XmlNode node, string attributeName, ref int value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    string strValue = attribute.Value;
                    value = int.Parse(strValue);
                    success = true;
                }
            }
            return success;
        }
        #endregion

        #region GetAttributeValue(node, name, value)
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value from an attribute at the specified node.
        /// </summary>
        /// <param name="node">The XmlNode from which this method will get the value of an attribute.</param>
        /// <param name="attributeName">Name of the attribute that will be read.</param>
        /// <param name="value">Attribute value read by this method</param>
        /// <returns>True if node is found and value is retrieved successfully.</returns>
        /// -----------------------------------------------------------------------------
        public static bool GetAttributeValue(XmlNode node, string attributeName, ref string value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    value = attribute.Value;
                    success = true;
                }
            }
            return success;
        }
}

Related Tutorials