Get Bool Attribute Or Throw exception : Attribute « XML « C# / C Sharp

C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
5.Data Types
7.Date Time
8.Design Patterns
9.Development Class
11.File Stream
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
20.Regular Expressions
22.Services Event
24.Web Services
26.Windows Presentation Foundation
C# / C Sharp » XML » Attribute 

Get Bool Attribute Or Throw exception

//Microsoft Public License (Ms-PL)
#region using
using System;
using System.Xml;

namespace DbmlManager.Lib.Utility
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>

  public class XmlUtil
    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;
    #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;


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 attribute value and convert to integer type
7.Get attribute value and convert to integer type or throw exception
8.Get Attribute and return Int64
9.Get Attribute and return Int64 or throw exception
10.Adds an XmlAttribute to a XmlNode
11.Get boolean value if an attribute is 1 or true
12.Attempts to get and cast attribute from an XmlElement
13.Attempts to get and convert an attribute from an XmlElement
14.Gets a string from an attribute or node.
15.Remove all the xml namespaces (xmlns) attributes in the xml string
16.Get Attribute With Conversion
17.Find a single Attribute of the type 'attributeType' from the supplied class
18.Create Xml Attribute, Node
19.Create Attribute, Text Element, Image Element,Read Image Element
20.Attribute Util
21.Find and get Attribute value
22.Finds an attribute with the given name in the given XML node and returns the attribute of the value.
23.Parse Attribute
24.Get attribute value (3)
25.Get int type attribute value
26.Get attribute value or throw exception
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  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.