Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extract XML values using XpathNavigator

Extract XML values using XpathNavigator

Scheduled Pinned Locked Moved C#
xmlhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    baranils
    wrote on last edited by
    #1

    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++;
      }
    
    P A A 3 Replies Last reply
    0
    • B baranils

      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++;
        }
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Probably, but I've never used one. I use an XmlDocument; the SelectNodes and SelectSingleNode methods use XPath.

      B 1 Reply Last reply
      0
      • P PIEBALDconsult

        Probably, but I've never used one. I use an XmlDocument; the SelectNodes and SelectSingleNode methods use XPath.

        B Offline
        B Offline
        baranils
        wrote on last edited by
        #3

        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++;
          }
        
        1 Reply Last reply
        0
        • B baranils

          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++;
            }
          
          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          xmlDoc.SelectSingleNode(liste/mobile); should give the same results.

          WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

          1 Reply Last reply
          0
          • B baranils

            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++;
              }
            
            A Offline
            A Offline
            Alan Balkany
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups