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. C# XPath Problems

C# XPath Problems

Scheduled Pinned Locked Moved C#
csharpxmlquestioncomhardware
2 Posts 2 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.
  • C Offline
    C Offline
    Chua Wen Ching
    wrote on last edited by
    #1

    Hi, I read from this tutorial at codeproject Question A: http://www.codeproject.com/csharp/GsXPathTutorial.asp regarding xpath.. but i try to apply in my situation, and can't get it work... just say this is my xml file: Device.xml ========= Titan123 Titan Suprema 13 BBB Hardware BBB DH 6 In my C# code, string devicePath = Application.StartupPath + @"\device.xml"; // open XmlTextReader ... int nMaxId = 0; // get maximum devices within XML script while(xmlDevice.Read()) { if (xmlDevice.IsStartElement() && xmlDevice.Name == "Device") { int nId = Int32.Parse(xmlDevice.GetAttribute("id")); if (nMaxId < nId) nMaxId = nId; } } } // Execute XPath here XPathDocument xdoc = new XPathDocument(devicePath); XPathNavigator nav = xdoc.CreateNavigator(); // loop into 2 possible devices and extract particular information for (int i = 1; i < nMaxId + 1; i++) { XPathNodeIterator nodeItor = nav.Select( "DeviceList/Device[@id='" + i + "']/Connection"); nodeItor.MoveNext(); TraverseChildren(nodeItor); } ... // from the article private void TraverseChildren(XPathNodeIterator nodeItor) { XPathNodeIterator igor = nodeItor.Clone(); igor.Current.MoveToFirstChild(); bool more = false; do { PrintNode(igor.Current); more = igor.Current.MoveToNext(); }while(more); } private void PrintNode(XPathNavigator nav) { MessageBox.Show("Value: " + nav.Value + " Type : " + nav.NodeType.ToString()); } From this solution: i get this instead: MessageBox1 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6 MessageBox2 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6 (repeat the same message - why?) Any help please? I want to get the Output of this: MessageBox1 - Value: Titan123 Titan Suprema 13 MessageBox2 - Value: BBB Hardware BBB H 6 Question B: By the way ho

    H 1 Reply Last reply
    0
    • C Chua Wen Ching

      Hi, I read from this tutorial at codeproject Question A: http://www.codeproject.com/csharp/GsXPathTutorial.asp regarding xpath.. but i try to apply in my situation, and can't get it work... just say this is my xml file: Device.xml ========= Titan123 Titan Suprema 13 BBB Hardware BBB DH 6 In my C# code, string devicePath = Application.StartupPath + @"\device.xml"; // open XmlTextReader ... int nMaxId = 0; // get maximum devices within XML script while(xmlDevice.Read()) { if (xmlDevice.IsStartElement() && xmlDevice.Name == "Device") { int nId = Int32.Parse(xmlDevice.GetAttribute("id")); if (nMaxId < nId) nMaxId = nId; } } } // Execute XPath here XPathDocument xdoc = new XPathDocument(devicePath); XPathNavigator nav = xdoc.CreateNavigator(); // loop into 2 possible devices and extract particular information for (int i = 1; i < nMaxId + 1; i++) { XPathNodeIterator nodeItor = nav.Select( "DeviceList/Device[@id='" + i + "']/Connection"); nodeItor.MoveNext(); TraverseChildren(nodeItor); } ... // from the article private void TraverseChildren(XPathNodeIterator nodeItor) { XPathNodeIterator igor = nodeItor.Clone(); igor.Current.MoveToFirstChild(); bool more = false; do { PrintNode(igor.Current); more = igor.Current.MoveToNext(); }while(more); } private void PrintNode(XPathNavigator nav) { MessageBox.Show("Value: " + nav.Value + " Type : " + nav.NodeType.ToString()); } From this solution: i get this instead: MessageBox1 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6 MessageBox2 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6 (repeat the same message - why?) Any help please? I want to get the Output of this: MessageBox1 - Value: Titan123 Titan Suprema 13 MessageBox2 - Value: BBB Hardware BBB H 6 Question B: By the way ho

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      If you have a question about an article here on this site, then use the message board at the bottom of the article. That's what the article-specific message boards are for. http://www.codeproject.com/csharp/GsXPathTutorial.asp[^] This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      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