Connecting Tech Pros Worldwide Forums | Help | Site Map

SelectNodes not working like I thought it would

=?Utf-8?B?QmV2IEthdWZtYW4=?=
Guest
 
Posts: n/a
#1: Oct 24 '08
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>Hostess</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.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(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
Guest
 
Posts: n/a
#2: Oct 24 '08

re: SelectNodes not working like I thought it would


Bev Kaufman wrote:
Quote:
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>Hostess</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.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(0).Value
You want a relative XPath expression selecting the SPEAKER child element
e.g.
TempStr = node.SelectSingleNode("SPEAKER").InnerText


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

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
=?Utf-8?B?QmV2IEthdWZtYW4=?=
Guest
 
Posts: n/a
#3: Oct 24 '08

re: SelectNodes not working like I thought it would


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

"Martin Honnen" wrote:
Quote:
Bev Kaufman wrote:
Quote:
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>Hostess</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.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(0).Value
>
You want a relative XPath expression selecting the SPEAKER child element
e.g.
TempStr = node.SelectSingleNode("SPEAKER").InnerText
>
>
If you use //SPEAKER then you always select down from the root node (the
document node).
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
>
Closed Thread


Similar .NET Framework bytes