Jon Cosby <no****@jcosby.com> wrote:
<snip>
The output is all of the values for the config nodes for all users. I
am trying to retrieve a single value for "jon".
I suspect you're making things harder than you need to by using
XPathNavigator. I find it's usually easier to use SelectSingleNode on
the document (or element). Here's a sample:
using System;
using System.Xml;
using System.Xml.XPath;
class Test
{
static void Main()
{
string xml = @"
<timer>
<config user='default'>
<systrayenabled>false</systrayenabled>
<systraytimer>1</systraytimer>
<logevents>false</logevents>
</config>
<config user='jon'>
<systrayenabled>true</systrayenabled>
<systraytimer>1</systraytimer>
<logevents>false</logevents>
</config>
</timer>
";
XmlDocument doc = new XmlDocument();
doc.LoadXml (xml);
XmlNode node = doc.SelectSingleNode("timer/config
[@user='jon']/systrayenabled");
Console.WriteLine (node.OuterXml);
}
}
Now, as to why it wasn't working for you: from the docs of
XPathNodeIterator:
<quote>
An XPathNodeIterator object returned by the XPathNavigator class is not
positioned on the first node in a selected set of nodes. A call to the
MoveNext method of the XPathNodeIterator class must be made to position
the XPathNodeIterator object on the first node in the selected set of
nodes.
</quote>
And indeed if you call MoveNext() before printing the result, you'll
find you get the results you expect. I'd still use SelectSingleNode
though :)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too