C#-Xml Recupérer élément xml par son id

maheox -  
 klarann -
Bonjour à tous !!

Ma question n'est pas trop compliquer s'il existe une solution :D

J'ai besoin de récupérer un élément xml grâce à son ID. J'ai déjà lu ce tuto que je conseil d'ailleurs.

Mon exemple est EXACTEMENT le même à une seule différence prêt:

Dans le point 4 lorsqu'il récupère son élément par son id il le fait avec un int. J'ai tout simplement besoin de le faire avec un string. Un peu comme s'il récupérait ses livres avec leur balise id "bk101" "bk102" ect..

Merci bien
A voir également:

3 réponses

maheox
 
0
chuka Messages postés 980 Statut Membre 378
 
Salut,
y'a peut-etre plus simple....mais c'est peut-etre une piste:
using System.Collections.Specialized;   

 public static OrderedDictionary DocList(string filename)   
        {   
            XmlDocument document = new XmlDocument();   
            try   
            {   
                document.Load(filename);   
            }   
            catch (Exception)   
            {   
                throw;   
            }   

            XmlNode node = document.DocumentElement;   
            try   
            {   
                XmlNodeList nodeList = node.SelectNodes("book");   
                OrderedDictionary o = new OrderedDictionary();   
                foreach (XmlNode n in nodeList)   
                {   
                    o.Add(n.Attributes["id"].Value, n);   
                }   
                return o;   
            }   
            catch (Exception)   
            {   
                throw;   
            }   
        }   
        public Book BookParsing(string filename, string id)   
        {   
            Book book = new Book();   
            OrderedDictionary nodeList = DocList(filename);   

            try   
            {   
                book.Author += (nodeList[id] as XmlNode).SelectNodes("author").Item(0).InnerText;   
                book.Title += (nodeList[id] as XmlNode).SelectNodes("title").Item(0).InnerText;   
                book.Genre += (nodeList[id] as XmlNode).SelectNodes("genre").Item(0).InnerText;   
                book.Price += (nodeList[id] as XmlNode).SelectNodes("price").Item(0).InnerText;   
                book.Publish_date += (nodeList[id] as XmlNode).SelectNodes("publish_date").Item(0).InnerText;   
                book.Description += (nodeList[id] as XmlNode).SelectNodes("description").Item(0).InnerText;   
            }   
            catch (Exception)   
            {   
                throw;   
            }   
            return book;   
        }   
public Book BookParsing(string filename, int id)   
        {   
            Book book = new Book();   
            OrderedDictionary nodeList = DocList(filename);   

            try   
            {   
                book.Author += (nodeList[id] as XmlNode).SelectNodes("author").Item(0).InnerText;   
                book.Title += (nodeList[id] as XmlNode).SelectNodes("title").Item(0).InnerText;   
                book.Genre += (nodeList[id] as XmlNode).SelectNodes("genre").Item(0).InnerText;   
                book.Price += (nodeList[id] as XmlNode).SelectNodes("price").Item(0).InnerText;   
                book.Publish_date += (nodeList[id] as XmlNode).SelectNodes("publish_date").Item(0).InnerText;   
                book.Description += (nodeList[id] as XmlNode).SelectNodes("description").Item(0).InnerText;   
            }   
            catch (Exception)   
            {   
                throw;   
            }   
            return book;   
        }   

Tu utilises une liste ordonnée...comme cela tu peux l'avoir par index ou string....
J'ai pas testé..mais ca devrait marcher....;)
@+
Ce n'est pas parce que certaines choses semblent inexplicables, qu'il faut faire semblant de les expliquer!
0
klarann
 
Pourrais tu stp me donner le lien du tuto dont tu parles? merci
0