Get Bool Attribute Or Throw exception

image_pdfimage_print
   
 

//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion

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

  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 &#039;{0}&#039; not specified in node &#039;{1}&#039;", attrib, node.Name));

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

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

      return returnVal;
    }
    #endregion
  }
}

   
     


This entry was posted in XML-RPC. Bookmark the permalink.