value in a variable for string method

Hi,

The Select() method of XPathNavigator takes an XPath expression in the form of a string. I have a string type variable 'name' which needs to be given to Select() method. How should I do it

name = "John";

Ideally, I should have something like

XPathNavigator nav = doc.CreateNavigator();
nav.Select("/root/level1/Name[contains(.,name)]");

Instead of name variable, its value should come into the Select() method.

How should I do it

Thank you.


Answer this question

value in a variable for string method

  • Gris

    This should work.

    nav.Select(string.Format("/root/level1/Name[contains(.,{0})]", name));


  • lg2578

    Hi,

    Thanks for the reply. There is a small correction.

    nav.Select(string.Format("/root/level1/Name[contains(.,'{0}')]", name));

    Since it expects a string type, there should be a single quotes.

  • value in a variable for string method