Xpath and arguments

J

Jeppe BS

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 ??
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Stefan

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

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