Difficulty with XPathNodeIterator

M

Michael Bray

I'm using an XPathNodeIterator (xpi) to navigate an xml document, and I'm
trying to use the Evaluate function to simply retrieve the value of a child
node, but it isn't working like I would expect...

The data is really simple...

<A>
<B name='xyz'>
<value>Text</value>
</B>
</A>

At some point, when my xpi.Current points to the B node (because
xpi.GetAttribute("name") = xyz) I want to get the text inside the <value>
child, so I call xpi.Current.Evaluate("value") which I would have thought
would give me the value, but it returns the xpi itself (in other words,
xpi.Evaluate("value") returns xpi.)

What am I missing??

-mdb
 
M

Martin Honnen

Michael said:
I'm using an XPathNodeIterator (xpi) to navigate an xml document, and I'm
trying to use the Evaluate function to simply retrieve the value of a child
node, but it isn't working like I would expect...

The data is really simple...

<A>
<B name='xyz'>
<value>Text</value>
</B>
</A>

At some point, when my xpi.Current points to the B node (because
xpi.GetAttribute("name") = xyz) I want to get the text inside the <value>
child, so I call xpi.Current.Evaluate("value") which I would have thought
would give me the value, but it returns the xpi itself (in other words,
xpi.Evaluate("value") returns xpi.)

What am I missing??

I think you want
xpi.Current.SelectSingleNode("value").Value
or if you want to use the Evaluate method then make sure your XPath
expression gives a string result
xpi.Current.Evaluate("string(value)")

Your XPath expression "value" selects all child elements named "value"
and such a node-set is represented as an XPathNodeIterator in the .NET
XPath model.
 
M

Michael Bray

I think you want
xpi.Current.SelectSingleNode("value").Value
or if you want to use the Evaluate method then make sure your XPath
expression gives a string result
xpi.Current.Evaluate("string(value)")

Your XPath expression "value" selects all child elements named "value"
and such a node-set is represented as an XPathNodeIterator in the .NET
XPath model.

Yes thanks - the SelectSingleNode works perfectly. I'm not familiar with
the XPath structure that you use in the second example, but I've got what I
need to make it work. Thanks!

-mdb
 

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

Similar Threads


Top