开发者

Get a node list which contains all Nodes who's child nodes count is 0

开发者 https://www.devze.com 2023-01-22 21:33 出处:网络
I have an xml file <RTT> <AgencyList> <agency Name=\"Bart\" Ditection=\"Outbound\"> <RouteList>

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>
    </开发者_StackOverflowAgencyList>
</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">


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消