Search code examples
c#xpathstring.formatxpathnodeiterator

Meaning of String.Format in C# in the following context?


I was just going through certain code...I wanted to know what the following code means

string format = "//User[UserName=\"{0}\" and EncryptPassword=\"{1}\"]";
string xpath = String.Format( format, userName, password );    

xpath is later used to create xPathNodeIterator object. I dont quite understand how String.Format is used and which node will the XPathNodeIterator will be iterating through if I iterate?


Solution

  • Read the details of String.Format. It

    Replaces each format item in a specified string with the text equivalent of a corresponding object's value.

    it means that xpath will contain format string where {0} in the format string will be replaced with value of username and {1} will be replaced with value of password

    Assuming that

    string userName = "Ehsan";
    string password = "Password";
    string format = "//User[UserName=\"{0}\" and EncryptPassword=\"{1}\"]";
    string xpath = String.Format(format, userName, password);  
    

    Xpath will be equivalent to

    //User[UserName="Ehsan" and EncryptPassword="Password"]