Search code examples
c#xmlselectsinglenode

how can i use a variable in this SelectSingleNode statement


how can i use a variable in this SelectSingleNode statement

   oldCd = root.SelectSingleNode("/students/student[id={0}]",id);

Solution

  • if id is an attribute of student element

    root.SelectSingleNode(String.Format("//students/student[@id='{0}']",id))
    

    if it is sub element

    root.SelectSingleNode(String.Format("//students/student[id[text()='{0}']]",id))