Lire la valeur d’un attribut et la convertir

Author:

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

/*
 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
  {

    public static string GetAttrib(XmlNode noeud, string attrib, string valDefaut)
    {
      XmlAttribute xmlAttrib = noeud.Attributes[attrib];
      if (xmlAttrib == null)
        return valDefaut;

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

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

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

      return returnVal;
    }
    #endregion
  }
}

Leave a Reply

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