473,320 Members | 2,180 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,320 software developers and data experts.

Xpath Help C#

7
Hi

I had a quick question - How do I use Xpath to search the following xml file? Or should I be using something else? Pls note that the file has xml empty tags.

xml file
Expand|Select|Wrap|Line Numbers
  1. <World>
  2. <Country Name ="Denmark"/>
  3. <Country Name ="Hungry"/>
  4. </World>
I tried the following code in C# ASP.net



Expand|Select|Wrap|Line Numbers
  1. XmlDocument doc = new XmlDocument();
  2. doc.Load("C:\\world.xml");
  3. XmlNodeList nodeList;
  4. XmlNode root = doc.DocumentElement;
  5. nodeList = root.SelectNodes("World/[Country Name='Denmark']");
and it showed the following error

Expression must evaluate to a node-set.The error occured at the last line(5)

I would like the node list to have 'Denmark'. I highly appreciate any kind of suggestion.

Thanks
Kim

--------------------------------------------------------------------------------
Oct 21 '07 #1
3 2060
newasp
7
HI
I figured it ....

The query should be
XmlNodeList nodeList = xmlDoc.SelectNodes("//Country[@Name = ' Denmark']");

Now my question is

Suppose I have a string variable that reads the string entered by the user in the textbox , how can I write the query without hard coding the country name?

Thanks
Kim
Oct 21 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
HI
I figured it ....

The query should be
XmlNodeList nodeList = xmlDoc.SelectNodes("//Country[@Name = ' Denmark']");

Now my question is

Suppose I have a string variable that reads the string entered by the user in the textbox , how can I write the query without hard coding the country name?

Thanks
Kim
Expand|Select|Wrap|Line Numbers
  1. XmlNodeList nodeList = xmlDoc.SelectNodes("//Country[@Name = '" + this.txtBoxCountryName + "']");
is that all you are looking for?
Alternatively you can use the String Builder class but that does most of the trick.
Oct 22 '07 #3
newasp
7
Hi Sasi,

Thank you very much. That's exactly what I have been looking for : ) and it worked.

Have a nice day
Kim
Oct 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
5
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
3
by: werD | last post by:
Hello I have an xml document that im currently using a forward only .net repeater on and using some xpath queries to display the data The xml is quite simple <?xml version="1.0"...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...

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.