0

I'm working with an API and retrieving the data in XML. Here's my XML:

<RTT>
  <AgencyList>
    <Agency Name="Caltrain" HasDirection="True" Mode="Rail">
      <RouteList>
        <Route Name="BABY BULLET" Code="BABY BULLET">
          <RouteDirectionList>
            <RouteDirection Code="SB2" Name="SOUTHBOUND TO TAMIEN">
              <StopList>
                <Stop name="Sunnyvale Caltrain Station" StopCode="70222">
                  <DepartureTimeList/>
                </Stop>
              </StopList>
            </RouteDirection>
            <RouteDirection Code="NB" Name="NORTHBOUND TO SAN FRANCISCO">
              <StopList>
                <Stop name="Sunnyvale Caltrain Station" StopCode="70221">
                  <DepartureTimeList>
                    <DepartureTime>69</DepartureTime>
                  </DepartureTimeList>
                </Stop>
              </StopList>
            </RouteDirection>
          </RouteDirectionList>
        </Route>
        <Route Name="LIMITED" Code="LIMITED">...</Route>
        <Route Name="LOCAL" Code="LOCAL">...</Route>
      </RouteList>
    </Agency>
  </AgencyList>
</RTT>

Not every DepartureTimeList will have a DepartureTime child node. Here's what I got so far, which retrieves the Route name:

List<string> trainType = new List<string>();
XDocument doc = XDocument.Load("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=0f01ac4a-bc16-46a5-8527-5abc79fee435&agencyName=Caltrain&stopName=" + DropDownList1.SelectedItem.Text.ToString());
doc.Save("times.xml");
string feed = doc.ToString();


XmlReader r = XmlReader.Create(new StringReader(feed));
r.ReadToFollowing("RouteList");
if (r.ReadToDescendant("Route"))
{
    do
    {
        trainType.Add(r.GetAttribute("Name"));
    } while (r.ReadToNextSibling("Route"));
}

I'm mostly interested in the departure time (if it exists) and I've been struggling all afternoon to try and parse it.

3
  • Did you try using LINQ to XML for query instead? Commented Nov 4, 2013 at 3:35
  • I've never actually used LINQ to XML for parsing XML. Commented Nov 4, 2013 at 3:40
  • Note that ASP.NET is clearly not relevant to this question. Commented Nov 4, 2013 at 5:50

1 Answer 1

2

Try this... Hopefully this will do it.

    XmlDocument doc = new XmlDocument();
    doc.Load("xml path");
    XmlNode node = doc.SelectSingleNode("/RTT");
    foreach (XmlNode nodes in node.SelectNodes(
        "/AgencyList/Agency Name/RouteList/Route"))
    {
        trainType.Add(r.GetAttribute("Name"));
        XmlNode s = nodes.SelectSingleNode("Route Name/RouteDirectionList/RouteDirection Code/StopList/Stop");
        if (s != null && s["DepartureTimeList"].HasChildNodes)
        {
            // do stuff here
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.