Extract XML values using XpathNavigator
-
Hello Is there a way to extract some values from an XML node using the XmlPathNavigator ? I need to extrat information from a XML file. I was first trying to use an XmlTextReader but the pure sequential access seeme a bit tedious to handle So I was trying to use the XpathDocument I use a XPathNavigator to browse each node of my document (see code below) But for each product I just need to extract a few information - Brand - Model - ID (there are a lot of other unneeded information) Is it possible to do that with the XpathNavigator ? Or do I need another approach ? Obviously I can write my own parser but I can't believe that there is no simple way with standard classes ? Thanks for any help !
XPathDocument xmldoc = new XPathDocument(p); XPathNavigator nav = xmldoc.CreateNavigator(); int j = 0; foreach (XPathNavigator product in nav.Select("liste/mobile")) { j++; }
-
Hello Is there a way to extract some values from an XML node using the XmlPathNavigator ? I need to extrat information from a XML file. I was first trying to use an XmlTextReader but the pure sequential access seeme a bit tedious to handle So I was trying to use the XpathDocument I use a XPathNavigator to browse each node of my document (see code below) But for each product I just need to extract a few information - Brand - Model - ID (there are a lot of other unneeded information) Is it possible to do that with the XpathNavigator ? Or do I need another approach ? Obviously I can write my own parser but I can't believe that there is no simple way with standard classes ? Thanks for any help !
XPathDocument xmldoc = new XPathDocument(p); XPathNavigator nav = xmldoc.CreateNavigator(); int j = 0; foreach (XPathNavigator product in nav.Select("liste/mobile")) { j++; }
Probably, but I've never used one. I use an XmlDocument; the SelectNodes and SelectSingleNode methods use XPath.
-
Probably, but I've never used one. I use an XmlDocument; the SelectNodes and SelectSingleNode methods use XPath.
Thanks I finaly found some solution The first think to care is also the granularity In my case every product embed a bloc_im node that contains everything So the first select must be on "liste/product/bloc_im" I was first doing "liste/product" and it take me a while to understand that first issue !
foreach (XPathNavigator product in nav.Select("liste/product/bloc\_im")) { string refORF = product.SelectSingleNode("ref").Value; string IdORF = product.SelectSingleNode("id").Value; string Brand= product.SelectSingleNode("Brand").Value; string modele = product.SelectSingleNode("model").Value; sw.WriteLine("{0}\\t{1}\\t{2}\\t{3}",Id,ref,Brand,modele); XPathNavigator xNav = product.SelectSingleNode("TACS"); foreach (XPathNavigator xNav1 in xNav.Select("TAC")) { string Tac = xNav1.Value; } j++; }
-
Hello Is there a way to extract some values from an XML node using the XmlPathNavigator ? I need to extrat information from a XML file. I was first trying to use an XmlTextReader but the pure sequential access seeme a bit tedious to handle So I was trying to use the XpathDocument I use a XPathNavigator to browse each node of my document (see code below) But for each product I just need to extract a few information - Brand - Model - ID (there are a lot of other unneeded information) Is it possible to do that with the XpathNavigator ? Or do I need another approach ? Obviously I can write my own parser but I can't believe that there is no simple way with standard classes ? Thanks for any help !
XPathDocument xmldoc = new XPathDocument(p); XPathNavigator nav = xmldoc.CreateNavigator(); int j = 0; foreach (XPathNavigator product in nav.Select("liste/mobile")) { j++; }
xmlDoc.SelectSingleNode(liste/mobile);
should give the same results.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Hello Is there a way to extract some values from an XML node using the XmlPathNavigator ? I need to extrat information from a XML file. I was first trying to use an XmlTextReader but the pure sequential access seeme a bit tedious to handle So I was trying to use the XpathDocument I use a XPathNavigator to browse each node of my document (see code below) But for each product I just need to extract a few information - Brand - Model - ID (there are a lot of other unneeded information) Is it possible to do that with the XpathNavigator ? Or do I need another approach ? Obviously I can write my own parser but I can't believe that there is no simple way with standard classes ? Thanks for any help !
XPathDocument xmldoc = new XPathDocument(p); XPathNavigator nav = xmldoc.CreateNavigator(); int j = 0; foreach (XPathNavigator product in nav.Select("liste/mobile")) { j++; }
I've just discovered LINQ to XML, which makes processing XML files very easy. You only need two classes (which become available when you have using System.Xml.Linq): XDocument and XElement. XDocument opens the XML file. XElement is the type of each node in the XML tree. Some of the XElement methods return a nice IEnumerable list that you can process with foreach. There's a lot more to LINQ to XML, but just these two classes enable you to easily do basic processing.