Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple de lecture d’attribut et gestion des exceptions

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

/*
 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
  {
 
    #region GetAttrib(XmlNode noeud, string attrib, string valDefaut)
    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;
    }
    #endregion
 
    #region GetInt64AttribOrThrow(XmlNode noeud, string attrib)
    public static Int64 GetInt64AttribOrThrow(XmlNode noeud, string attrib)
    {
      string val = GetAttrib(noeud, attrib, null);
      if (val == null)
        throw new Exception(String.Format("Attribute '{0}' non spécifié dans le noeud '{1}'", attrib, noeud.Name));
 
      if (val == null || val == string.Empty)
        return 0;
 
      Int64 returnVal = 0;
      Int64.TryParse(val, out returnVal);
 
      return returnVal;
    }
    #endregion
  }
}

No comments yet.

Leave a Comment