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

Xpath and arguments

got this method which works fine.

public string getNodeByName(string name)
{
XmlDocument personDB = new XmlDocument();
personDB.Load("persons.xml");

XmlNode root = personDB.DocumentElement;

XmlNodeList searchList = root.SelectNodes("//person[name='Casper']");

foreach (XmlNode node in searchList) {
output += node.OuterXml;
}
return output;
}

My problem is:
How can i pass a name to the XPath line ? instead of casper i wanne use
the name parameter ??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
2 1686
Jeppe,

I would use a simple string replacement, like this:

// Search the list.
XmlNodeList searchList = root.SelectNodes("//person[name='" + name + "']");

Or, if you want to be more efficient:

// Search the list.
XmlNodeList searchList =
root.SelectNodes(String.Format("//person[name='{0}']", name));

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeppe BS" <an*******@devdex.com> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl...
got this method which works fine.

public string getNodeByName(string name)
{
XmlDocument personDB = new XmlDocument();
personDB.Load("persons.xml");

XmlNode root = personDB.DocumentElement;

XmlNodeList searchList = root.SelectNodes("//person[name='Casper']");

foreach (XmlNode node in searchList) {
output += node.OuterXml;
}
return output;
}

My problem is:
How can i pass a name to the XPath line ? instead of casper i wanne use
the name parameter ??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Hi Jeppe
How about: XmlNodeList searchList = root.SelectNodes("//person[name='" +
parameter + "']"); ? ;-)
Greetings

"Jeppe BS" <an*******@devdex.com> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl...
got this method which works fine.

public string getNodeByName(string name)
{
XmlDocument personDB = new XmlDocument();
personDB.Load("persons.xml");

XmlNode root = personDB.DocumentElement;

XmlNodeList searchList = root.SelectNodes("//person[name='Casper']");

foreach (XmlNode node in searchList) {
output += node.OuterXml;
}
return output;
}

My problem is:
How can i pass a name to the XPath line ? instead of casper i wanne use
the name parameter ??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #3

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

Similar topics

0
by: bdinmstig | last post by:
I am building various framework components for my team to use in development, and one of those components is a Facade for reading/writing user preferences. The idea is that preference settings...
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...
1
by: Sonu Kapoor | last post by:
Hi, I would like to use xpath with xslt. I know that this should be possible, but I dont get it ! Here is what I have tried so far: ==================================== my xml file:...
8
by: Lawrence Oluyede | last post by:
Is there a way to treat html tags like simple text? I explain myself, if I have a bunch of xml like <content type="application/xhtml+xml" xml:base="http://loluyede.blogspot.com" xml:lang="en-US"...
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...
3
by: Steve | last post by:
Hello.. I have been trying to figure this out for the last hour, but I am baffled... I'm hoping someone can help! I'm trying to write an xpath expression to find the position of the node with...
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>
8
by: Jean-François Michaud | last post by:
Who in the name of #%@! thought this one out?? I noticed this behavior when trying to debug a problem I was having. I used this logical expression and some XPATH in a specific sequence of...
14
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.