Parcourir un fichier XML avec XPath

nounours21_6 Messages postés 255 Date d'inscription mercredi 3 novembre 2004 Statut Membre Dernière intervention 21 avril 2009 - 21 avril 2009 à 11:24
billou_13 Messages postés 860 Date d'inscription jeudi 4 mars 2004 Statut Membre Dernière intervention 19 août 2014 - 21 avril 2009 à 13:41
Bonjour,

J'ai un problème avec mon code pour parcourir un fichier XML.

Voici mon code :
XPathDocument document = new XPathDocument("fichier.xml");
                XPathNavigator navigator = document.CreateNavigator();
                
                XPathNodeIterator nodes = navigator.Select("Report/table1/Detail_Collection");
                nodes.MoveNext();
                XPathNavigator nodesNavigator = nodes.Current;

                XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
                
                while (nodesText.MoveNext())
                    this.label4.Text += "\r" + nodesText.Current.Value;


et voici la structure du fichier XML :
<html><report xmlns="..." xsi="" name="R3' - Report Computer File"><table3><detail_collection><detail textbox15="Run ID : 2000 - &quot;Pilote du 11 decembre 2008&quot;"></detail></detail_collection></table3><table2><detail_collection><detail textbox7="Computer Name : K-PC-IND01047 / Owner : ..."></detail></detail_collection></table2><table1><detail_collection><detail textbox4="K-PC-IND01047" textbox5="fichier1.doc" textbox14="748544"><detail textbox4="K-PC-IND01047" textbox5="fichier2.doc" textbox14="162"><detail textbox4="K-PC-IND01047" textbox5="fichier3.doc" textbox14="68608"></detail></detail></detail></detail_collection></table1></report>


Mon but serait de récupérer tous les éléments de table1/detail_collection afin de les ajouter dans une collection typée...
mais le problème est que la node courante est toujours "root" et donc ne passe pas dans la boucle while.

Merci d'avance

Enjoy @+</html>

2 réponses

nounours21_6 Messages postés 255 Date d'inscription mercredi 3 novembre 2004 Statut Membre Dernière intervention 21 avril 2009
21 avril 2009 à 11:44
Revoila mon message formaté...

---------------------------------------------------


Bonjour,


J'ai un problème avec mon code pour parcourir un fichier XML.


Voici mon code :
XPathDocument document = new XPathDocument("fichier.xml");

XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("Report/table1/Detail_Collection");

nodes.MoveNext();

XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.element, false);

while (nodesText.MoveNext())

{ this.label4.Text += "\r" + nodesText.Current.Value; }



et voici la structure du fichier XML :
<?xml version="1.0"?>

<Report xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." Name="R3' - Report Computer File">

--<table3>

----<Detail_Collection>

------<Detail textbox15="Run ID : 2000 - "Pilote du 11 decembre 2008"" />

----</Detail_Collection>

--</table3>

--<table2>

----<Detail_Collection>

------<Detail textbox7="Computer Name : K-PC-IND01047 / Owner : ..." />

----</Detail_Collection>

--</table2>

--<table1>

----<Detail_Collection>

------<Detail textbox4="K-PC-IND01047" textbox5="fichier1.doc" textbox14="748544" />

------<Detail textbox4="K-PC-IND01047" textbox5="fichier2.doc" textbox14="162" />

------<Detail textbox4="K-PC-IND01047" textbox5="fichier3.doc" textbox14="68608" />

----</Detail_Collection>

--</table1>

</Report>



Mon but serait de récupérer tous les éléments de table1/detail_collection afin de les ajouter dans une collection typée...

mais le problème est que la node courante est toujours "root" et donc ne passe pas dans la boucle while.


Merci d'avance

enjoy @+
0
billou_13 Messages postés 860 Date d'inscription jeudi 4 mars 2004 Statut Membre Dernière intervention 19 août 2014 29
21 avril 2009 à 13:41
Bonjour,

Pour ce faire, voici quelques liens codes-sources qui te seront très utiles:
http://www.csharpfr.com/tutoriaux/XPATH-LANGUAGE_536.aspx
http://www.csharpfr.com/tutoriaux/CSHARP-COMMENT-RECUPERER-FACILEMENT-DONNEES-FICHIER-XML_267.aspx

Bon développement,

Billou_13
Bask En Force

--------------------------------------------------------------------
Connaître la réponse est une chose, savoir pourquoi en est une autre
---------------------
0
Rejoignez-nous