Get attribute value or throw exception : Attribute « XML « C# / C Sharp






Get attribute value or throw exception

 

using System;
using System.Xml;


public class XmlUtil
{


    #region GetAttrib(XmlNode node, string attrib, string defVal)
    public static string GetAttrib(XmlNode node, string attrib, string defVal)
    {
        XmlAttribute xmlAttrib = node.Attributes[attrib];
        if (xmlAttrib == null)
            return defVal;

        string val = xmlAttrib.Value;
        return (val == null) ? defVal : val;
    }
    #endregion

    #region GetBoolAttrib(XmlNode node, string attrib, bool defVal)
    public static bool GetBoolAttrib(XmlNode node, string attrib, bool defVal)
    {
        XmlAttribute xmlAttrib = node.Attributes[attrib];
        if (xmlAttrib == null)
            return defVal;

        string val = xmlAttrib.Value;
        if (val == null || val == string.Empty)
            return defVal;

        bool returnVal = (val == "1" || val.ToLower() == "true");

        return returnVal;
    }
    #endregion

    #region GetBoolAttribOrThrow(XmlNode node, string attrib)
    public static bool GetBoolAttribOrThrow(XmlNode node, string attrib)
    {
        string val = GetAttrib(node, attrib, null);
        if (val == null)
            throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

        if (val == null || val == string.Empty)
            return false;

        bool returnVal = (val == "1" || val.ToLower() == "true");

        return returnVal;
    }
    #endregion

    #region GetIntAttrib(XmlNode node, string attrib, int defVal)
    public static int GetIntAttrib(XmlNode node, string attrib, int defVal)
    {
        XmlAttribute xmlAttrib = node.Attributes[attrib];
        if (xmlAttrib == null)
            return defVal;

        string val = xmlAttrib.Value;
        if (val == null || val == string.Empty)
            return defVal;

        int returnVal = defVal;
        int.TryParse(val, out returnVal);

        return returnVal;
    }
    #endregion

    #region GetIntAttribOrThrow(XmlNode node, string attrib)
    public static int GetIntAttribOrThrow(XmlNode node, string attrib)
    {
        string val = GetAttrib(node, attrib, null);
        if (val == null)
            throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

        if (val == null || val == string.Empty)
            return 0;

        int returnVal = 0;
        int.TryParse(val, out returnVal);

        return returnVal;
    }
    #endregion

    #region GetInt64Attrib(XmlNode node, string attrib, Int64 defVal)
    public static Int64 GetInt64Attrib(XmlNode node, string attrib, Int64 defVal)
    {
        XmlAttribute xmlAttrib = node.Attributes[attrib];
        if (xmlAttrib == null)
            return defVal;

        string val = xmlAttrib.Value;
        if (val == null || val == string.Empty)
            return defVal;

        Int64 returnVal = defVal;
        Int64.TryParse(val, out returnVal);

        return returnVal;
    }
    #endregion

    #region GetInt64AttribOrThrow(XmlNode node, string attrib)
    public static Int64 GetInt64AttribOrThrow(XmlNode node, string attrib)
    {
        string val = GetAttrib(node, attrib, null);
        if (val == null)
            throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

        if (val == null || val == string.Empty)
            return 0;

        Int64 returnVal = 0;
        Int64.TryParse(val, out returnVal);

        return returnVal;
    }
    #endregion

}

   
  








Related examples in the same category

1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value (2)
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Get Attribute With Conversion
18.Find a single Attribute of the type 'attributeType' from the supplied class
19.Create Xml Attribute, Node
20.Create Attribute, Text Element, Image Element,Read Image Element
21.Attribute Util
22.Find and get Attribute value
23.Finds an attribute with the given name in the given XML node and returns the attribute of the value.
24.Parse Attribute
25.Get attribute value (3)
26.Get int type attribute value
27.Read an XML file from the xml element and the xml attribute
28.Create and set attribute
29.Adds a new attribute to the given target element.
30.Get Text Attribute
31.Get Int Attribute