Xpath and arguments

  • Thread starter Thread starter Jeppe BS
  • Start date Start date
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 ??
 
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.
 
Hi Jeppe
How about: XmlNodeList searchList = root.SelectNodes("//person[name='" +
parameter + "']"); ? ;-)
Greetings
 
Back
Top