C# vs xml
i'm sorry for my bad english.
i'm an italian student and i've a problem with the execution of a xpath expression.
i've a document named book.xml in C:\
and i don't know for what reason this code i wrote doesn't work:
------------------------------------------------------------------------------
XmlTextReader reader = new XmlTextReader(@"c:\book.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
// nodeList = root.SelectNodes("/bookstore/book/price[text()>'9.00']"); <-- this query works
nodeList = root.SelectNodes("/bookstore/book/price[text()='11.99']"); // <-- this query doesn't work
Console.Write(nodeList.Count.ToString());
---------------------------------------------------------------------------
this is the structure of book.xml
bookstore
book
price 11.99 /price
author .. author
title .. /title
/book
...other books
/bookstore
if i can't solve this problem my work doesn't go on :(
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
joe2 pointsPosted Nov 11, 2005, 6:12 AM EST
If this is your Xml structure:
<bookstore>
<book>
<price>11.99</price>
<author>Author One</author>
<title>Book One</title>
</book>
<book>
<price>12.99</price>
<author>Author Two</author>
<title>Book Two</title>
</book>
</bookstore>
There's nothing wrong with your code. I'd maybe drop the use of the XmlTextReader though:
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\book.xml");
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/price[text()>'9.00']"); // <-- this query works
//nodeList = root.SelectNodes("/bookstore/book/price[text()='11.99']"); // <-- this query works too
Console.Write(nodeList.Count.ToString());
Console.ReadLine();
Joe
gino gino3 pointsPosted Nov 11, 2005, 9:49 AM EST
there was an error in the xml file, not in the code ehehehe...i'm too much stupid!!!