begin process at 2008 05 16 21:55:18
1 173 770 membres
577 nouveaux aujourd'hui
13 973 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

XPATH : UTILISEZ DES REQUÊTES PARAMÉTRÉES


Information sur la source

Catégorie :.NET Source .NET ( DotNet ) Classé sous : xpath, paramètre, xsltcontext, ixsltcontextvariable Niveau : Débutant Date de création : 18/03/2007 Vu / téléchargé: 4 294 / 204

Note :
9,67 / 10 - par 3 personnes
9,67 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (3)
Ajouter un commentaire et/ou une note


Description

Il s'agit des sources utilisées pour le post "XPath : utilisez des requêtes paramétrées" que vous trouverez ici : http://blogs.codes-sources.com/coq/archive/2007/03/18/xpath-utilisez-des-requ-tes-param-tr-es.aspx

Ces sources contiennent notamment le contexte personnalisé : la classe CustomContext.

Source

  • using System;
  • using System.Xml;
  • using System.Xml.Xsl;
  • using System.Xml.XPath;
  • using System.Collections.Generic;
  • namespace PersonsXPath
  • {
  • class CustomContext : XsltContext
  • {
  • #region Constructeurs
  • public CustomContext()
  • : base()
  • {
  • }
  • public CustomContext(NameTable table)
  • : base(table)
  • {
  • }
  • #endregion Constructeurs
  • #region Champs
  • /// <summary>
  • /// Contient les variables.
  • /// </summary>
  • private Dictionary<String, IXsltContextVariable> _variables
  • = new Dictionary<String, IXsltContextVariable>();
  • #endregion Champs
  • #region Implémentation de XsltContext
  • public override int CompareDocument(String baseUri, String nextbaseUri)
  • {
  • // Nous utilisons simplement une comparaison de chaîne.
  • return String.CompareOrdinal(baseUri, nextbaseUri);
  • }
  • public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
  • {
  • return true;
  • }
  • public override bool Whitespace
  • {
  • get
  • {
  • return true;
  • }
  • }
  • public override IXsltContextFunction ResolveFunction(
  • String prefix,
  • String name,
  • XPathResultType[] ArgTypes)
  • {
  • // Inutile dans notre cas, nous ne nous intéressons qu'aux variables.
  • return null;
  • }
  • public override IXsltContextVariable ResolveVariable(String prefix, String name)
  • {
  • IXsltContextVariable var = null;
  • this._variables.TryGetValue(name, out var);
  • return var;
  • }
  • #endregion Implémentation de XsltContext
  • #region Gestion des paramètres
  • public void AddVariable(String name, Object value)
  • {
  • #region Vérification des paramètres
  • if (name == null)
  • throw new ArgumentNullException("name");
  • else if (name.Length < 0)
  • throw new ArgumentException("name");
  • if (value == null)
  • throw new ArgumentNullException("value");
  • #endregion Vérification des paramètres
  • this._variables[name] = new Variable(name, value);
  • }
  • public Boolean RemoveVariable(String name)
  • {
  • #region Vérification des paramètres
  • if (name == null)
  • throw new ArgumentNullException("name");
  • else if (name.Length < 0)
  • throw new ArgumentException("name");
  • #endregion Vérification des paramètres
  • return this._variables.Remove(name);
  • }
  • public void ClearVariables()
  • {
  • this._variables.Clear();
  • }
  • #endregion Gestion des paramètres
  • private class Variable : IXsltContextVariable
  • {
  • #region Constructeur
  • /// <summary>
  • /// Initialise une nouvelle instance de <see cref="Variable"/>.
  • /// </summary>
  • /// <param name="name"></param>
  • /// <param name="value"></param>
  • /// <exception cref="ArgumentNullException">
  • /// <c>name</c> est une référence nulle.
  • /// ou
  • /// <c>value</c> est une référence nulle.
  • /// </exception>
  • /// <exception cref="ArgumentException">
  • /// <c>name</c> est une chaîne vide.
  • /// </exception>
  • public Variable(String name, Object value)
  • {
  • #region Vérification des paramètres
  • if (name == null)
  • throw new ArgumentNullException("name");
  • else if (name.Length < 0)
  • throw new ArgumentException("name");
  • if (value == null)
  • throw new ArgumentNullException("value");
  • #endregion Vérification des paramètres
  • this._name = name;
  • this._value = value;
  • this.DetermineType();
  • }
  • #endregion Constructeur
  • #region Champs
  • /// <summary>
  • /// Nom de la variable.
  • /// </summary>
  • private String _name;
  • /// <summary>
  • /// Valeur de la variable.
  • /// </summary>
  • private Object _value;
  • /// <summary>
  • /// Type de la variable.
  • /// </summary>
  • private XPathResultType _type;
  • #endregion Champs
  • #region Propriétés
  • /// <summary>
  • /// Obtient ou définit le nom de la variable.
  • /// </summary>
  • public String Name
  • {
  • get
  • {
  • return this._name;
  • }
  • set
  • {
  • this._name = value;
  • }
  • }
  • /// <summary>
  • /// Obtient ou définit la valeur de la variable.
  • /// </summary>
  • public Object Value
  • {
  • get
  • {
  • return this._value;
  • }
  • set
  • {
  • this._value = value;
  • }
  • }
  • #endregion Propriétés
  • #region Implémentation de IXsltContextVariable
  • public object Evaluate(XsltContext xsltContext)
  • {
  • return this._value;
  • }
  • public Boolean IsLocal
  • {
  • get
  • {
  • return false;
  • }
  • }
  • public Boolean IsParam
  • {
  • get
  • {
  • return false;
  • }
  • }
  • public XPathResultType VariableType
  • {
  • get
  • {
  • return this._type;
  • }
  • }
  • #endregion Implémentation de IXsltContextVariable
  • #region Méthodes utilitaires
  • /// <summary>
  • /// Permet de déterminer le type de la valeur.
  • /// </summary>
  • private void DetermineType()
  • {
  • // Détermination des types :
  • // String : XPathResultType.String
  • // Boolean : XPathResultType.Boolean
  • // XPathNavigator : XPathResultType.Navigator
  • // XPathNodeIterator : XPathResultType.NodeSet
  • // Double : XPathResultType.Number
  • // Convertible en Double : XPathResultType.Number
  • // Autres : XPathResultType.Any
  • if (this._value is String)
  • this._type = XPathResultType.String;
  • else if (this._value is Boolean)
  • this._type = XPathResultType.Boolean;
  • else if (this._value is XPathNavigator)
  • this._type = XPathResultType.Navigator;
  • else if (this._value is XPathNodeIterator)
  • this._type = XPathResultType.NodeSet;
  • else if (this._value is Double)
  • this._type = XPathResultType.Number;
  • else if (this._value is IConvertible)
  • {
  • // Si le type n'est pas double mais implémente IConvertible,
  • // nous tentons la conversion.
  • try
  • {
  • this._value = Convert.ToDouble(this._value);
  • this._type = XPathResultType.Number;
  • }
  • catch
  • {
  • this._type = XPathResultType.Any;
  • }
  • }
  • else
  • this._type = XPathResultType.Any;
  • }
  • #endregion Méthodes utilitaires
  • }
  • }
  • }
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Collections.Generic;

namespace PersonsXPath
{
    class CustomContext : XsltContext
    {
        #region Constructeurs

        public CustomContext()
            : base()
        {
        }

        public CustomContext(NameTable table)
            : base(table)
        {
        }

        #endregion Constructeurs

        #region Champs

        /// <summary>
        /// Contient les variables.
        /// </summary>
        private Dictionary<String, IXsltContextVariable> _variables
            = new Dictionary<String, IXsltContextVariable>();

        #endregion Champs

        #region Implémentation de XsltContext

        public override int CompareDocument(String baseUri, String nextbaseUri)
        {
            // Nous utilisons simplement une comparaison de chaîne.
            return String.CompareOrdinal(baseUri, nextbaseUri);
        }

        public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
        {
            return true;
        }

        public override bool Whitespace
        {
            get
            {
                return true;
            }
        }

        public override IXsltContextFunction ResolveFunction(
            String prefix,
            String name,
            XPathResultType[] ArgTypes)
        {
            // Inutile dans notre cas, nous ne nous intéressons qu'aux variables.
            return null;
        }

        public override IXsltContextVariable ResolveVariable(String prefix, String name)
        {
            IXsltContextVariable var = null;
            this._variables.TryGetValue(name, out var);
            return var;
        }

        #endregion Implémentation de XsltContext

        #region Gestion des paramètres

        public void AddVariable(String name, Object value)
        {
            #region Vérification des paramètres

            if (name == null)
                throw new ArgumentNullException("name");
            else if (name.Length < 0)
                throw new ArgumentException("name");

            if (value == null)
                throw new ArgumentNullException("value");

            #endregion Vérification des paramètres

            this._variables[name] = new Variable(name, value);
        }

        public Boolean RemoveVariable(String name)
        {
            #region Vérification des paramètres

            if (name == null)
                throw new ArgumentNullException("name");
            else if (name.Length < 0)
                throw new ArgumentException("name");

            #endregion Vérification des paramètres

            return this._variables.Remove(name);
        }

        public void ClearVariables()
        {
            this._variables.Clear();
        }

        #endregion Gestion des paramètres

        private class Variable : IXsltContextVariable
        {
            #region Constructeur

            /// <summary>
            /// Initialise une nouvelle instance de <see cref="Variable"/>.
            /// </summary>
            /// <param name="name"></param>
            /// <param name="value"></param>
            /// <exception cref="ArgumentNullException">
            /// <c>name</c> est une référence nulle. 
            /// ou 
            /// <c>value</c> est une référence nulle. 
            /// </exception>
            /// <exception cref="ArgumentException">
            /// <c>name</c> est une chaîne vide.
            /// </exception>
            public Variable(String name, Object value)
            {
                #region Vérification des paramètres

                if (name == null)
                    throw new ArgumentNullException("name");
                else if (name.Length < 0)
                    throw new ArgumentException("name");

                if (value == null)
                    throw new ArgumentNullException("value");

                #endregion Vérification des paramètres

                this._name = name;
                this._value = value;

                this.DetermineType();
            }

            #endregion Constructeur

            #region Champs

            /// <summary>
            /// Nom de la variable.
            /// </summary>
            private String _name;

            /// <summary>
            /// Valeur de la variable.
            /// </summary>
            private Object _value;

            /// <summary>
            /// Type de la variable.
            /// </summary>
            private XPathResultType _type;

            #endregion Champs

            #region Propriétés

            /// <summary>
            /// Obtient ou définit le nom de la variable.
            /// </summary>
            public String Name
            {
                get
                {
                    return this._name;
                }

                set
                {
                    this._name = value;
                }
            }

            /// <summary>
            /// Obtient ou définit la valeur de la variable.
            /// </summary>
            public Object Value
            {
                get
                {
                    return this._value;
                }

                set
                {
                    this._value = value;
                }
            }

            #endregion Propriétés

            #region Implémentation de IXsltContextVariable

            public object Evaluate(XsltContext xsltContext)
            {
                return this._value;
            }

            public Boolean IsLocal
            {
                get
                {
                    return false;
                }
            }

            public Boolean IsParam
            {
                get
                {
                    return false;
                }
            }

            public XPathResultType VariableType
            {
                get
                {
                    return this._type;
                }
            }

            #endregion Implémentation de IXsltContextVariable

            #region Méthodes utilitaires

            /// <summary>
            /// Permet de déterminer le type de la valeur.
            /// </summary>
            private void DetermineType()
            {
                // Détermination des types : 
                // String                : XPathResultType.String
                // Boolean               : XPathResultType.Boolean
                // XPathNavigator        : XPathResultType.Navigator
                // XPathNodeIterator     : XPathResultType.NodeSet
                // Double                : XPathResultType.Number
                // Convertible en Double : XPathResultType.Number
                // Autres                : XPathResultType.Any

                if (this._value is String)
                    this._type = XPathResultType.String;
                else if (this._value is Boolean)
                    this._type = XPathResultType.Boolean;
                else if (this._value is XPathNavigator)
                    this._type = XPathResultType.Navigator;
                else if (this._value is XPathNodeIterator)
                    this._type = XPathResultType.NodeSet;
                else if (this._value is Double)
                    this._type = XPathResultType.Number;
                else if (this._value is IConvertible)
                {
                    // Si le type n'est pas double mais implémente IConvertible, 
                    // nous tentons la conversion.
                    try
                    {
                        this._value = Convert.ToDouble(this._value);
                        this._type = XPathResultType.Number;
                    }
                    catch
                    {
                        this._type = XPathResultType.Any;
                    }
                }
                else
                    this._type = XPathResultType.Any;
            }

            #endregion Méthodes utilitaires
        }
    }
}
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de romagny13 le 18/03/2007 12:43:03

    Ouuuf alors la je dis "trop" merci
    moi qui ai pas une seconde a moi
    ca va peut etre m'avancer pas mal a ce niveau car j'ai toujours pas regarder sur msdn comment les utiliser
    merci

  • signaler à un administrateur
    Commentaire de Bidou le 20/03/2007 15:49:42 administrateur CS

    J'espère que ce coup de pub n'est pas trop mal placé ;-p
    http://www.csharpfr.com/tutorial.aspx?ID=536

  • signaler à un administrateur
    Commentaire de coq le 21/03/2007 18:48:09 administrateur CS

    Lol, fait toi plaisir.

Ajouter un commentaire

Appels d'offres

Pub



CalendriCode

Mai 2008
LMMJVSD
   1234
567891011
12131415161718
19202122232425
262728293031 

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Boutique

Boutique de goodies CodeS-SourceS