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. XML / XSL
  4. Trouble with VB.NET and XPath

Trouble with VB.NET and XPath

Scheduled Pinned Locked Moved XML / XSL
xmltutorialquestioncsharp
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.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    I was told that I may be able to get better help in this forum so I am reposting my question here. I'm trying to write a program that navigates an XML file with XPath but I'm having trouble if the XML file is structured a certain way. For example here is the code that navigates the XML file:

    Dim XDoc as XPathDocument
    Dim xmlNav as XPathNavigator
    Dim xmlNI as XPathNodeIterator

    XDoc = new XPathDocument("C:\sop.xml")
    xmlNav = XDoc.CreateNavigator
    xmlNI = xmlNav.Select("/SOP_FILE/QuestionList/Question")

    While xmlNI.MoveNext
    MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
    End While

    This was just a test function I wrote to try out because this is my first time using XPath; hence it doesn't do much. Now, if I the XML is formated as follows, a message box is never displayed because the xmlNI count is zero:

    <?xml version="1.0" encoding="utf-8"?>
    <SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <QuestionList>
    <Question Number="1">
    <Text>Has a risk assessment been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="2" />
    </Question>
    <Question Number="2">
    <Text>Has an inspection been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="3" />
    <Choice Text="No" Index="2" PointsTo="4" />
    </Question>
    <Question Number="3">
    <Text>Is the property lead-based paint free?</Text>
    </Question>
    </QuestionList>
    </SOP_FILE>

    However if the second line of the XML file is changed to:

    <SOP_FILE>

    Instead of:

    <SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    Then the test function works properly and loops through all the questions. Can someone please explain to me why this is happening and how to modify the test function so it will work properly. Thanks in advance for any help.

    G 1 Reply Last reply
    0
    • D Dominick Marciano

      I was told that I may be able to get better help in this forum so I am reposting my question here. I'm trying to write a program that navigates an XML file with XPath but I'm having trouble if the XML file is structured a certain way. For example here is the code that navigates the XML file:

      Dim XDoc as XPathDocument
      Dim xmlNav as XPathNavigator
      Dim xmlNI as XPathNodeIterator

      XDoc = new XPathDocument("C:\sop.xml")
      xmlNav = XDoc.CreateNavigator
      xmlNI = xmlNav.Select("/SOP_FILE/QuestionList/Question")

      While xmlNI.MoveNext
      MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
      End While

      This was just a test function I wrote to try out because this is my first time using XPath; hence it doesn't do much. Now, if I the XML is formated as follows, a message box is never displayed because the xmlNI count is zero:

      <?xml version="1.0" encoding="utf-8"?>
      <SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <QuestionList>
      <Question Number="1">
      <Text>Has a risk assessment been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="2" />
      </Question>
      <Question Number="2">
      <Text>Has an inspection been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="3" />
      <Choice Text="No" Index="2" PointsTo="4" />
      </Question>
      <Question Number="3">
      <Text>Is the property lead-based paint free?</Text>
      </Question>
      </QuestionList>
      </SOP_FILE>

      However if the second line of the XML file is changed to:

      <SOP_FILE>

      Instead of:

      <SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">

      Then the test function works properly and loops through all the questions. Can someone please explain to me why this is happening and how to modify the test function so it will work properly. Thanks in advance for any help.

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      When you have a namespace in your XML document, you have to account for it in your XPath expression whether it is the default namespace or not. Look up "XmlNamespaceManager" in the System.Xml namespace.

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      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