Gets XML attribute or returns a default value - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Gets XML attribute or returns a default value

Demo Code


using System.Xml;
using System.Text;
using System.Collections.Generic;
using System;/* w w w. j a  va  2 s.  c om*/

public class Main{
        /// <summary>
    /// Gets the attribute or returns a default value
    /// </summary>
    /// <param name="xNode"></param>
    /// <param name="attributeName"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static string RequiredParseXmlAttribute(XmlNode xNode, string attributeName)
    {
        var attr = xNode.Attributes[attributeName];
        if ((attr == null) || (string.IsNullOrWhiteSpace(attr.Value)))
        {
            throw new Exception("No xml attribute found for " + attributeName);
        }

        return attr.Value;
    }
}

Related Tutorials