Get XML Attribute by name - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Get XML Attribute by name

Demo Code


using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  ww  w.j a v  a 2s  .c  om

public class Main{
        public static string GetAttribute(this XmlNode root, string attributeName)
            {
                if (null == root)
                {
                    return null;
                }

                var attrs = root.Attributes;
                if (null == attrs)
                {
                    return null;
                }

                XmlNode attr = attrs.GetNamedItem(attributeName);
                if (null == attr)
                {
                    return null;
                }

                return attr.Value;
            }
}

Related Tutorials