C#-Xml Recupérer élément xml par son id
Fermé
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
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:
- C#-Xml Recupérer élément xml par son id
- Xml download - Télécharger - Édition & Programmation
- Recuperer message whatsapp supprimé - Guide
- Office xml handler - Télécharger - Traitement de texte
- Driveimage xml - Télécharger - Sauvegarde
- Recuperer video youtube - Guide
3 réponses
Désolé j'ai oublier le lien pour le tuto :) https://yanngarit.wordpress.com/2010/04/02/c-xml-parsing-dun-fichier-xml/
chuka
Messages postés
965
Date d'inscription
samedi 11 octobre 2008
Statut
Membre
Dernière intervention
29 juillet 2010
378
Modifié par chuka le 15/06/2010 à 22:19
Modifié par chuka le 15/06/2010 à 22:19
Salut,
y'a peut-etre plus simple....mais c'est peut-etre une piste:
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!
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!