Obtenir la valeur d’un Attribut

Author:

Utilisation,de,la,classe,'XmlTextReader',pour,lire,un,document,XML
{filelink=16394}

//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 description for XmlUtil.
  /// 

  #endregion

  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 GetAttribOrThrow(XmlNode node, string attrib)
    public static string GetAttribOrThrow(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));

      return val;
    }
    #endregion

  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *