inserting C# variables into XPath expressions

G

Guest

Thanks in advance to anyone who can help :)

I'm trying to extract a specific NodeSet out of an XML file; however, I'd
like to include a C# string variable as part of the XPATH statement; so far
I've been unsuccessful and I'd like to know if this is even possible and if
so how to do it.
An example:

XMLDOC.SELECTNODES(/root/child[@attrname=\"csharpvariable\"])
(sees the literal 'csharpvariable', not the actual underlying C# variable
value..

I've also tried:
XMLDOC.SELECTNODES(/root/child[@attrname=\"" + csharpvariable + "\"]")
and it still simply sees the literal 'csharpvariable'
 
L

Lowell Heddings

studen77 said:
Thanks in advance to anyone who can help :)

I'm trying to extract a specific NodeSet out of an XML file; however, I'd
like to include a C# string variable as part of the XPATH statement; so far
I've been unsuccessful and I'd like to know if this is even possible and if
so how to do it.
An example:

XMLDOC.SELECTNODES(/root/child[@attrname=\"csharpvariable\"])
(sees the literal 'csharpvariable', not the actual underlying C# variable
value..

I've also tried:
XMLDOC.SELECTNODES(/root/child[@attrname=\"" + csharpvariable + "\"]")
and it still simply sees the literal 'csharpvariable'

I think you forgot the first " char... selectnodes takes a string argument.

XMLDOC.SELECTNODES("/root/child[@attrname=\"" + csharpvariable + "\"]")
-------------------^

Lowell
 
G

Guest

Hi,

Since XmlDocument.SelectNodes accepts a string try a double quote at the
beginning of the argument.

XmlDocument.SelectNodes("/bookstore/book/@bk:ISBN");


Hope this helps.
 
G

Guest

My mistake;
Yes the arguments for the SelectNodes method were enclosed in quotes:
XMLDOC.SELECTNODES("/root/child[@attrname=\""+csharpvariablename+"\"]")

it still will not recognize the underlying value of the variable
'csharpvariablename', only the literal constant
 
G

Guest

Hi,

From the code sample that you provided I cannot get the code to reproduce
the behavior that you are describing. I have created a sample app that
attempts to mimic your code. You should be able to create a console
application and then cut and paste the code below. Don’t forget to adjust
the path to the xml document.

I hope that this helps!

-----------------------------Sample Console App (watch for line wrap)

using System;
using System.Xml;
using System.Xml.XPath;

namespace ConsoleTestApp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string csharpvariablename = "story";
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:\books.xml");

XmlNodeList nodeList;
XmlElement root = xdoc.DocumentElement;
nodeList =
xdoc.SelectNodes("/bookstore/book[@genre=\""+ csharpvariablename +"\"]");
foreach(XmlNode n in nodeList)
Console.WriteLine(n.InnerText.ToString());

}
}
}

----------------------------- XML Document - Books.xml

<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore >
<book genre="story" publicationdate="1997">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="story" publicationdate="1991">
<title>Emma</title>
<author>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
</author>
<price>19.95</price>
</book>
</bookstore>
 
G

Guest

Thanks for the assistance guys (or girls) :)

THe issue was the XmlDocument.Load function; I was trying to point to an xml
file located in another project; .NET did not return an error but NOR did it
return any XML, which is why I created this post. I placed the XML file
locally, and made its path the argument in the xmldocument.load() method. It
does indeed accept c# variables in the Xpath statement!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top