This project has moved and is read-only. For the latest updates, please go here.

Is this right?

Topics: Developer Forum, Project Management Forum, User Forum
Apr 6, 2009 at 11:28 PM

I have a xml document like this:
    <village name="test">
      <strategy class="Farm">
        <troop type="doll">10</troop>

    <village name="default">
      <strategy class="Farm">
        <troop type="teddybear">12</troop>

I want to grab the troop-nodes inside the village-nodes inside the strategy-nodes. So my code is this:

String xml = new StreamReader("configuration.xml").ReadToEnd();
HtmlNode farmnode = xpath.getNode(xml, "//village[@name='test']//strategy[@class='Farm']");
foreach(HtmlNode child in farmnode.SelectNodes("//troop"))
                Console.WriteLine( child.Attributes["type"].Value );

There is only one troop-node inside the village with the name "test", so my output should be "doll" however my output is:
- doll
- teddybear

Can someone tell me why the code is acting up like this?

May 18, 2009 at 8:00 AM

"//" starts from the top always. You should do "//village[@name='test']/strategy[@class='Farm']/troop"