tags:

views:

32

answers:

1

I have an xml file

<RTT>
    <AgencyList>
        <agency Name="Bart" Ditection="Outbound">
            <RouteList>
                <Route Name="Fremont" Direction="test">
                    <Stop ID="25" Name="BayFare"/>
                </Route>
                <Route Name="SFO" Direction="Test"/>
                <Route Name="SFO" Direction="Test"/>
            </RouteList>
        </agency>
    </AgencyList>
</RTT>

I would like to know how to get a node list which contains all the Route which has no child nodes using C#.

E.g The Node list should contain only

<Route  Name="SFO" Direction "Test">
<Route  Name="SFO" Direction "Test">
+2  A: 

The XPATH expression: //Route[not(node())] will address those Route elements.

It can be used in C# with something like this:

XmlDocument xml = new XmlDocument();
xml.Load("RTT.xml"); //adjust correct path/filename

XmlNodeList xnList = xml.SelectNodes("//Route[not(node())]");
foreach (XmlNode xn in xnList)
{
  //Do something with the selected elements
}
Mads Hansen