SelectNodes not working like I thought it would

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QmV2IEthdWZtYW4=?=

    SelectNodes not working like I thought it would

    I've been trying to teach myself XML. I have an XML file with contents like
    this (from a play):
    <SPEECH>
    <SPEAKER>SLY</SPEAKER>
    <LINE>I'll pheeze you, in faith.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>Hostes s</SPEAKER>
    <LINE>A pair of stocks, you rogue!</LINE>
    </SPEECH>

    I've written the following code:
    Dim nodeSpeeches As XmlNodeList
    Dim node As XmlNode
    nodeSpeeches = xmlDoc.SelectNo des("//SPEECH")
    For Each node In nodeSpeeches
    TempStr = node.SelectSing leNode("//SPEAKER").Child Nodes(0).Value
    aIndex = GetSpeakerIndex (TempStr)
    ... Load data based on index
    Next
    It doesn't work. The value of SPEAKER is always the first occurrence in the
    entire XML document instead of the current SPEECH node. The LINE count is
    always the number of LINE elements in the entire document instead of the
    current SPEECH.
    It was my understanding that
    For Each node In nodeSpeeches
    would render node into an XML fragment holding only one <SPEECH>...</SPEECH>
    for each iteration.
    Why doesn't this work?



  • Martin Honnen

    #2
    Re: SelectNodes not working like I thought it would

    Bev Kaufman wrote:
    I've been trying to teach myself XML. I have an XML file with contents like
    this (from a play):
    <SPEECH>
    <SPEAKER>SLY</SPEAKER>
    <LINE>I'll pheeze you, in faith.</LINE>
    </SPEECH>
    >
    <SPEECH>
    <SPEAKER>Hostes s</SPEAKER>
    <LINE>A pair of stocks, you rogue!</LINE>
    </SPEECH>
    >
    I've written the following code:
    Dim nodeSpeeches As XmlNodeList
    Dim node As XmlNode
    nodeSpeeches = xmlDoc.SelectNo des("//SPEECH")
    For Each node In nodeSpeeches
    TempStr = node.SelectSing leNode("//SPEAKER").Child Nodes(0).Value
    You want a relative XPath expression selecting the SPEAKER child element
    e.g.
    TempStr = node.SelectSing leNode("SPEAKER ").InnerTex t


    If you use //SPEAKER then you always select down from the root node (the
    document node).
    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?QmV2IEthdWZtYW4=?=

      #3
      Re: SelectNodes not working like I thought it would

      Thank you. It works now. On to the next bug.

      "Martin Honnen" wrote:
      Bev Kaufman wrote:
      I've been trying to teach myself XML. I have an XML file with contents like
      this (from a play):
      <SPEECH>
      <SPEAKER>SLY</SPEAKER>
      <LINE>I'll pheeze you, in faith.</LINE>
      </SPEECH>

      <SPEECH>
      <SPEAKER>Hostes s</SPEAKER>
      <LINE>A pair of stocks, you rogue!</LINE>
      </SPEECH>

      I've written the following code:
      Dim nodeSpeeches As XmlNodeList
      Dim node As XmlNode
      nodeSpeeches = xmlDoc.SelectNo des("//SPEECH")
      For Each node In nodeSpeeches
      TempStr = node.SelectSing leNode("//SPEAKER").Child Nodes(0).Value
      >
      You want a relative XPath expression selecting the SPEAKER child element
      e.g.
      TempStr = node.SelectSing leNode("SPEAKER ").InnerTex t
      >
      >
      If you use //SPEAKER then you always select down from the root node (the
      document node).
      --
      >
      Martin Honnen --- MVP XML

      >

      Comment

      Working...