Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : XPath query problem w-attributes



Dave Lech
2/18/2005 9:05:01 AM
In a C# app I have an XPath query where I am trying to return a single node
based on the value of 2 different attributes.
The xml looks something like this:

<TESTS>
<TEST SAMPLE_NUMBER="1" TEST_NUMBER="1"/>
<TEST SAMPLE_NUMBER="1" TEST_NUMBER="2"/>
<TEST SAMPLE_NUMBER="2" TEST_NUMBER="1"/>
<TEST SAMPLE_NUMBER="2" TEST_NUMBER="2"/>
</TESTS>


From the TESTS node I am calling the SelectSingleNode() method like this:

XmlNode testNode = testsNode.SelectSingleNode("TEST[@SAMPLE_NUMBER = 1 and
@TEST_NUMBER = 1]");

I keep getting an "Invalid token" exception. What is wrong?

Any help is greatly appreciated.

Dave Lech



Martin Honnen
2/18/2005 6:32:01 PM


[quoted text, click to view]

Does the C# compile?
If the exception occurs at runtime on which line does it occur, the
SelectSingleNode or earlier?

--

Martin Honnen
yingzile NO[at]SPAM online.microsoft.com
2/28/2005 7:22:38 PM
Hi Martin,

The XPath you specified is not quite right. The following code worked.
Notice you'll need to specify the root node or you can use the double
forward slash to get the node:

XmlDocument oDoc = new XmlDocument();
XmlTextReader reader = new XmlTextReader("test.xml");
oDoc.Load(reader);
XmlNode testNode = oDoc.SelectSingleNode("/TESTS/TEST[@SAMPLE_NUMBER = 1
and @TEST_NUMBER = 1]");

or XmlNode testNode =
oDoc.SelectSingleNode("//TEST[@SAMPLE_NUMBER = 1 and @TEST_NUMBER = 1]");

You'll find lots of usefull XPath reference here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html
/xmrefxpathsyntax.asp

Thanks,
---Yingzi Le
LongHorn SDK Team

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
[quoted text, click to view]

AddThis Social Bookmark Button