Xpath Help C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newasp
    New Member
    • Oct 2007
    • 7

    Xpath Help C#

    Hi

    I had a quick question - How do I use Xpath to search the following xml file? Or should I be using something else? Pls note that the file has xml empty tags.

    xml file
    Code:
    <World>
    <Country Name ="Denmark"/>
    <Country Name ="Hungry"/>
    </World>
    I tried the following code in C# ASP.net



    Code:
    XmlDocument doc = new XmlDocument();
    doc.Load("C:\\world.xml");
    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList = root.SelectNodes("World/[Country Name='Denmark']");
    and it showed the following error

    Expression must evaluate to a node-set.The error occured at the last line(5)

    I would like the node list to have 'Denmark'. I highly appreciate any kind of suggestion.

    Thanks
    Kim

    --------------------------------------------------------------------------------
  • newasp
    New Member
    • Oct 2007
    • 7

    #2
    HI
    I figured it ....

    The query should be
    XmlNodeList nodeList = xmlDoc.SelectNo des("//Country[@Name = ' Denmark']");

    Now my question is

    Suppose I have a string variable that reads the string entered by the user in the textbox , how can I write the query without hard coding the country name?

    Thanks
    Kim

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #3
      Originally posted by newasp
      HI
      I figured it ....

      The query should be
      XmlNodeList nodeList = xmlDoc.SelectNo des("//Country[@Name = ' Denmark']");

      Now my question is

      Suppose I have a string variable that reads the string entered by the user in the textbox , how can I write the query without hard coding the country name?

      Thanks
      Kim
      [CODE=cpp]XmlNodeList nodeList = xmlDoc.SelectNo des("//Country[@Name = '" + this.txtBoxCoun tryName + "']");[/CODE]
      is that all you are looking for?
      Alternatively you can use the String Builder class but that does most of the trick.

      Comment

      • newasp
        New Member
        • Oct 2007
        • 7

        #4
        Thank you

        Hi Sasi,

        Thank you very much. That's exactly what I have been looking for : ) and it worked.

        Have a nice day
        Kim

        Comment

        Working...