473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Navigating by attribute in XPath

This code shows all values for the config node. I'm trying to retrieve
the value for systrayenabled for attrib "jon". Something wrong?
sXmlPath = Application.StartupPath.ToString() + "\\settings.xml";
XmlDocument xmlConfig = new XmlDocument();
xmlConfig.Load(sXmlPath);
XPathNavigator navigator = xmlConfig.CreateNavigator();

XPathNodeIterator iterator =
navigator.Select("timer/config[@user='jon']/systrayenabled");
MessageBox.Show(iterator.Current.Value);
May 31 '06 #1
7 2022
Hello Jon,

What's the problem?
I'd be better if you showed you xml

JC> This code shows all values for the config node. I'm trying to
JC> retrieve the value for systrayenabled for attrib "jon". Something
JC> wrong?
JC>
JC> sXmlPath = Application.StartupPath.ToString() + "\\settings.xml";
JC> XmlDocument xmlConfig = new XmlDocument();
JC> xmlConfig.Load(sXmlPath);
JC> XPathNavigator navigator = xmlConfig.CreateNavigator();
JC> XPathNodeIterator iterator =
JC> navigator.Select("timer/config[@user='jon']/systrayenabled");
JC> MessageBox.Show(iterator.Current.Value);
JC>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 31 '06 #2
Michael Nemtsev wrote:
Hello Jon,

What's the problem?
I'd be better if you showed you xml

JC> This code shows all values for the config node. I'm trying to
JC> retrieve the value for systrayenabled for attrib "jon". Something
JC> wrong?
JC>
JC> sXmlPath = Application.StartupPath.ToString() + "\\settings.xml";
JC> XmlDocument xmlConfig = new XmlDocument();
JC> xmlConfig.Load(sXmlPath);
JC> XPathNavigator navigator = xmlConfig.CreateNavigator();
JC> XPathNodeIterator iterator =
JC> navigator.Select("timer/config[@user='jon']/systrayenabled");
JC> MessageBox.Show(iterator.Current.Value);
JC>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

The output is all of the values for the config nodes for all users. I
am trying to retrieve a single value for "jon".
<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>

May 31 '06 #3
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
May 31 '06 #4

Jon Skeet wrote:
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


That works, but I'm using the XPathNavigator to modify the xml
document. I can navigate with a "MoveToChild/MoveToNext" sequence, but
I can't query values.

May 31 '06 #5
Jon Cosby <no****@jcosby.com> wrote:
That works, but I'm using the XPathNavigator to modify the xml
document.
Any reason not to modify the DOM model directly?
I can navigate with a "MoveToChild/MoveToNext" sequence, but
I can't query values.


If you call MoveNext() with the code you provided, then
iterator.Current.Value prints out "true" which is what you want, isn't
it?

--
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
May 31 '06 #6
Jon wrote:
Jon Cosby <no****@jcosby.com> wrote:
That works, but I'm using the XPathNavigator to modify the xml
document.


Any reason not to modify the DOM model directly?
I can navigate with a "MoveToChild/MoveToNext" sequence, but
I can't query values.


If you call MoveNext() with the code you provided, then
iterator.Current.Value prints out "true" which is what you want, isn't
it?

--
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


Not exactly what I meant. I need something like

navigator.MoveToNode("timesynch/config[@user='jon']/systrayenabled");
navigator.SetValue("Okay");

May 31 '06 #7
Jon Cosby <no****@jcosby.com> wrote:
Not exactly what I meant. I need something like

navigator.MoveToNode("timesynch/config[@user='jon']/systrayenabled");
navigator.SetValue("Okay");


So why not use SelectSingleNode (specifying the text node) and then set
the value to "Okay"?

Anyway, if you *do* genuinely need to use XPathNavigator, calling
MoveNext() to start with after calling Select should set you straight.

--
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
May 31 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the...
0
by: J E E | last post by:
Hi, Why can't I create a key and keyref with an attribute declared in an attribute group? Code below describes what I'm trying to accomplish. <!-- An attribute group --> <xs:attributeGroup...
9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
2
by: GIMME | last post by:
For starters .... If : //input is the xpath syntax to see if an element with an attribute named start_date exists ... Then what is the xpath syntax to return elements having
4
by: Abhinav | last post by:
Hi, I have the following XML : <checkIn bug="1111111"> <regression> </regression> </checkIn> How do i get the value "1111111" for bug ?
2
by: Isz | last post by:
Hi Group: I would like to know if it is possible to change the name of an attribute to something else. My setup is like this: I have serveral SQL tables that I nest and join so that it all...
5
by: requeth | last post by:
I am trying to create (another) type of report and I have been beating my head against the wall for several minutes now. Here's the problem: <CMS27423400_2140CA>...
2
by: pstachy | last post by:
Hi again! I have another issue. I would like the attribute of the tag <invoice> to be unique. Made the following schema but unfortunately it doesn't validate. Could someone please indicate what is...
2
by: Grant Merwitz | last post by:
Hi, i'm not sure if this is the appropriate topic. But here goes: I am currently bulding a site menu from a xml file. I have an attribute that indicates whether an option is active or not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.