Is this right?

Topics: Developer Forum, Project Management Forum, User Forum
Apr 6, 2009 at 10:28 PM
Hi!

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

    <village name="default">
      <strategy class="Farm">
        <troop type="teddybear">12</troop>
        <target>Test</target>
      </strategy>
    </village>
  </server>
</configuration>

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?

Thanks
Coordinator
May 18, 2009 at 7:00 AM

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