Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get value of attribute (XPath)?

6 views
Skip to first unread message

LJB

unread,
Oct 7, 2009, 8:14:10 AM10/7/09
to
I've been trying to return the value of an attribute named "server" but keep
getting an XPathSelectionIterator. What do I need to change? The following
returns "1" which is what I expected.
Console.WriteLine(nav.Evaluate("count(//location[@office='JAC'])"));
I'm thinking it should be something like
Console.WriteLine(nav.Evaluate("//location[@office='JAC']/@server"));
but that doesn't give me the value.

thanks
LJB


Martin Honnen

unread,
Oct 7, 2009, 10:09:35 AM10/7/09
to

If you want to access a single node you might want to use
SelectSingleNode instead of Evaluate, that way you get an XPathNavigator
and can access the Value property e.g.

XPathNavigator server =
nav.SelectSingleNode("//location[@office='JAC']/@server");
if (server != null) // XPath found node
{
Console.WriteLine(server.Value);
}
else // handle case that no node was found
{
//
}


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

LJB

unread,
Oct 7, 2009, 11:40:04 AM10/7/09
to

"Martin Honnen" <maho...@yahoo.de> wrote in message
news:%23VY7Pf1...@TK2MSFTNGP05.phx.gbl...

THANK YOU. It works perfectly.


0 new messages