using System; using System.Windows.Forms; using System.Xml; using System.Xml.XPath; using System.Collections;
/// <summary> /// Cette classe permet de retourner la valeur d'une expression XPAth /// </summary> class XPathNav { private static string XMLFilePath; static XPathNav() { XMLFilePath = @".\MonAppli.exe.config"; } /// <summary> /// Methode vierge permettant d'activer la classe statique /// </summary> public static void init(){} /// <summary> /// Récupère la valeur de l'attribut du noeud recherché dans le fichier de configuration /// </summary> /// <param name="xPathString">Expression XPath de recherche du noeud</param> /// <param name="attribute">Attribut à rechercher</param> /// <returns>Une ArrayList contenant la liste des attributs recherchés</returns> public static ArrayList SearchXPathNavigator(string xPathString, string attribute) { // Initilisation des variables XPathDocument xpathDoc; XPathNavigator xpathNavigator; XPathNodeIterator xpathNodeIterator; XPathExpression expr; ArrayList listOfAttributes = new ArrayList(); // Parcours du fichier XML try { xpathDoc = new XPathDocument(XMLFilePath); xpathNavigator = xpathDoc.CreateNavigator(); expr = xpathNavigator.Compile(xPathString); xpathNodeIterator = xpathNavigator.Select(expr); while (xpathNodeIterator.MoveNext()) { // On récupère l'attribut listOfAttributes.Add(xpathNodeIterator.Current.GetAttribute(attribute, "")); } } catch (Exception e) { } return listOfAttributes; } }
|