public class Program { public static void Main(string[] args) { XPathDocument xPathDocu = new XPathDocument("test.xml"); XPathNavigator navigator = xPathDocu.CreateNavigator(); string xPathExpr = "//CONTENT[@Code='T1' or @Code='T2' or @Code='T3']"; XPathNodeIterator nodes = navigator.Select(navigator.Compile(xPathExpr)); while (nodes.MoveNext()) { string str1 = nodes.Current.GetAttribute("Code", String.Empty); nodes.Current.MoveToFirstChild(); string str2 = nodes.Current.InnerXml; Console.WriteLine(str1 + " " + str2); } } }
while (nodes.MoveNext()) { string str1 = nodes.Current.GetAttribute("Code", String.Empty); nodes.Current.MoveToFollowing("Hours", String.Empty); string str2 = nodes.Current.InnerXml; Console.WriteLine(str1 + " " + str2); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Xml; using System.Xml.XPath;
XPathDocument XPathDocu = new XPathDocument("fichier.xml"); XPathNavigator Navigator; XPathNodeIterator Nodes; Navigator = XPathDocu.CreateNavigator(); XPathNodeIterator Nodes2; ExpXPath = "//CONTENT[@Code='T1' or @Code='T2' or @Code='T3']"; Nodes = Navigator.Select(Navigator.Compile(ExpXPath)); String t = ""; while (Nodes.MoveNext()) { Nodes2 = Nodes.Clone(); Nodes2.Current.MoveToFirstAttribute(); // se place au début Nodes2.Current.MoveToNextAttribute(); // et now pile sur Code t = Nodes2.Current.Value; Console.WriteLine("{0}", "niveau=" + Int32.Parse(t.Replace("T", "").ToString())); Nodes.Current.MoveToFirstChild(); Console.WriteLine("{0}", "Nom=" + Nodes.Current.Value); }
nodes.Current.MoveToFirstChild();
<CONTENT ART="4" Code="41"> <Texte>Lorem ipsum</Texte> <Quantity>35.7</Quantity> <MontantHT>62.79</MontantHT> <Hours>17.85</Hours> </CONTENT>