473,382 Members | 1,717 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,382 software developers and data experts.

inserting C# variables into XPath expressions

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'

Nov 16 '05 #1
5 1900
studen77 wrote:
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
Nov 16 '05 #2
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.
Nov 16 '05 #3
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
Nov 16 '05 #4
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>


Nov 16 '05 #5
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!

"Brian Brown" wrote:
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>

Nov 16 '05 #6

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

Similar topics

3
by: axial | last post by:
Is there a way to define a variable by resolving other variables? I'm trying to build an "uber-variable" from user-selected components. I have control over the XML and can change it as needed. ...
1
by: rustyfancy | last post by:
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 =...
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.