Using Variables in XML Xpath Expressions

  • Thread starter Thread starter rustyfancy
  • Start date Start date
R

rustyfancy

I have a textBox that contains a string values. I need to take this
string and use it as criteria for an XPath Expression. Here's what I
have. Why isn't it working???

string s = textBox1.Text;
XPathExpression expr;
expr = myXPathNavigator.Compile("/NewDataSet/note[@to=$s]");

----Matt
 
Because XPath does not "compile" and run like code, it does not know about
your local CLR variables. Where did you find that you can use $var to work
with CLR variables?

You just need to include the text in your XPath expression:

XPathNavigator.Compile(String.Format("/NewDataSet/note[@to=\"{0}\"]", s));

And make sure there are no " in s before you do that (or find out how to
escape them in XPath).

Jerry

rustyfancy said:
I have a textBox that contains a string values. I need to take this
string and use it as criteria for an XPath Expression. Here's what I
have. Why isn't it working???

string s = textBox1.Text;
XPathExpression expr;
expr = myXPathNavigator.Compile("/NewDataSet/note[@to=$s]");

----Matt



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
Back
Top