selecting the VALUE of an XML element with XPathNavigation...

D

darrel

I've done a bit of transoforming XML in ASP.net by passing it to an XSL file
and returning the results.

I'm not trying to navigate an XML file and just return one value. I could
use XSL for this, but seems to be overkill to just return one value.

This is what I have thus far:

dim filename As String =
(MapPath("/usercontrols/navigation/siteStructure.xml"))
Dim xDoc As XPathDocument = New XPathDocument(filename)
Dim xNav1 as XPathNavigator = xDoc.CreateNavigator()
xNav1.Select("linkTitle")
dim strLinkTitle as String = xNav1.value

The problem is that xNav1.value returns the entire Node. I just want the
value of the element 'lnkTitle' as a string. I'm completely stumped as to
how to get that via asp.net.

-Darrel
 
D

darrel

I think I *can't* do this with xpathnavigator. Is that correct? Do I need to
use an XMLReader instead? If so, can I navigate with the navigator then
select the element with the reader?

-Darrel
 
B

bruce barker

an XPath returns a node or collection of nodes:

the following xml frag:

<a id="a1" b="d">hello</a>

if an XPath query returns the node "a", it will have two attribute children
nodes ("id" and "b")and a child text node ("hello"). you are probably
interested in the childs value not the parents.

-- bruce (sqlwork.com)
 
D

darrel

if an XPath query returns the node "a", it will have two attribute
children
nodes ("id" and "b")and a child text node ("hello"). you are probably
interested in the childs value not the parents.

ARRRRRRRRRGGGGGGGGGGHHHHHHHHHHH!!!!

Yes, you are absolutely right. Duh. I always forget that the value-of *is* a
child of the element.

Dang XML terminology!

OK, can I ask for a bit of help with the syntax?

I have this:

xNav1.Select("//linkTitle[parent::linkID = '" & intPageID & "']")

Which should find the linkTitle that has a parent that has a child of linkID
with the value of intPageID

Once I navigate to that, I want to grab the child text element of linkTitle.
So, I have this:

dim strLinkTitle as String = xNav1.MoveToFirstChild.ToString

But that returns a boolean true/false as opposed to the value.

I also tried changing the xpath query:

xNav1.Select("//linkTitle[parent::linkID = '" & intPageID &
"']/linkTitle")

but that, again, returns the entire node it seems.

my brain hurts.
 
B

bruce barker

MoveToFirstChild returns a boolean. you will need to find node, movetochild,
then get value from the navigator.



darrel said:
if an XPath query returns the node "a", it will have two attribute children
nodes ("id" and "b")and a child text node ("hello"). you are probably
interested in the childs value not the parents.

ARRRRRRRRRGGGGGGGGGGHHHHHHHHHHH!!!!

Yes, you are absolutely right. Duh. I always forget that the value-of *is* a
child of the element.

Dang XML terminology!

OK, can I ask for a bit of help with the syntax?

I have this:

xNav1.Select("//linkTitle[parent::linkID = '" & intPageID & "']")

Which should find the linkTitle that has a parent that has a child of linkID
with the value of intPageID

Once I navigate to that, I want to grab the child text element of linkTitle.
So, I have this:

dim strLinkTitle as String = xNav1.MoveToFirstChild.ToString

But that returns a boolean true/false as opposed to the value.

I also tried changing the xpath query:

xNav1.Select("//linkTitle[parent::linkID = '" & intPageID &
"']/linkTitle")

but that, again, returns the entire node it seems.

my brain hurts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top