473,612 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SELECTNO DES(/root/child[@attrname=\"csh arpvariable\"])
(sees the literal 'csharpvariable ', not the actual underlying C# variable
value..

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

Nov 16 '05 #1
5 1911
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.SELECTNO DES(/root/child[@attrname=\"csh arpvariable\"])
(sees the literal 'csharpvariable ', not the actual underlying C# variable
value..

I've also tried:
XMLDOC.SELECTNO DES(/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.SELECTNO DES("/root/child[@attrname=\"" + csharpvariable + "\"]")
-------------------^

Lowell
Nov 16 '05 #2
Hi,

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

XmlDocument.Sel ectNodes("/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.SELECTNO DES("/root/child[@attrname=\""+c sharpvariablena me+"\"]")

it still will not recognize the underlying value of the variable
'csharpvariable name', 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.XPat h;

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

XmlNodeList nodeList;
XmlElement root = xdoc.DocumentEl ement;
nodeList =
xdoc.SelectNode s("/bookstore/book[@genre=\""+ csharpvariablen ame +"\"]");
foreach(XmlNode n in nodeList)
Console.WriteLi ne(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.Loa d 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.loa d() 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.XPat h;

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

XmlNodeList nodeList;
XmlElement root = xdoc.DocumentEl ement;
nodeList =
xdoc.SelectNode s("/bookstore/book[@genre=\""+ csharpvariablen ame +"\"]");
foreach(XmlNode n in nodeList)
Console.WriteLi ne(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
1790
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. The user is allowed to specify one to three data fields from a list and up to three text fields to use as the pattern for creating chapter names: <ChapterNameDefaults>
1
421
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 = textBox1.Text; XPathExpression expr; expr = myXPathNavigator.Compile("/NewDataSet/note"); ----Matt
4
9118
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
0
8173
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6082
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4111
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1699
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1416
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.