0

私は HTTPWebrequest を実行するプログラムに取り組んでおり、応答として次の XML データを取得します。(現時点では、応答をメッセージボックスに入れています)

<OperatorRef>STB</OperatorRef>
<MonitoredCall>
  <AimedDepartureTime>2013-03-21T11:41:00.000Z</AimedDepartureTime>
  <ExpectedDepartureTime>2013-03-21T11:44:27.000Z</ExpectedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T11:47:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T11:47:03.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>53</PublishedLineName>
    <DirectionName>Wootton</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T11:49:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T11:49:44.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>42</PublishedLineName>
    <DirectionName>Flitwick</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T11:49:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T11:50:26.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>1</PublishedLineName>
    <DirectionName>Kempston</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T11:53:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T11:52:45.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T11:59:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T11:59:03.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>1</PublishedLineName>
    <DirectionName>Kempston</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:05:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T12:04:45.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>42</PublishedLineName>
    <DirectionName>The Green</DirectionName>
    <OperatorRef>GP</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:09:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:11:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T12:11:03.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>1</PublishedLineName>
    <DirectionName>Kempston</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:17:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T12:16:45.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>53</PublishedLineName>
    <DirectionName>Wootton</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:19:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T12:19:44.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:23:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>1</PublishedLineName>
    <DirectionName>Kempston</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:29:00.000Z</AimedDepartureTime>
      <ExpectedDepartureTime>2013-03-21T12:28:45.000Z</ExpectedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>52</PublishedLineName>
    <DirectionName>Wharley End, Cranfield University main gate</DirectionName>
    <OperatorRef>bed-5100</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:33:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>44</PublishedLineName>
    <DirectionName>Ampthill, Alameda Walk</DirectionName>
    <OperatorRef>bed-153</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:33:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>STB</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:35:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>1</PublishedLineName>
    <DirectionName>Kempston</DirectionName>
    <OperatorRef>bed-5100</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:41:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>2</PublishedLineName>
    <DirectionName>Elstow P+R</DirectionName>
    <OperatorRef>bed-5100</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:46:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>42</PublishedLineName>
    <DirectionName>Flitwick, Woodlands Middle School</DirectionName>
    <OperatorRef>bed-5100</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:48:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
<MonitoredStopVisit>
  <RecordedAtTime>2013-03-21T11:40:13.514Z</RecordedAtTime>
  <MonitoringRef>020035811</MonitoringRef>
  <MonitoredVehicleJourney>
    <FramedVehicleJourneyRef>
      <DataFrameRef>-</DataFrameRef>
      <DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
    </FramedVehicleJourneyRef>
    <VehicleMode>bus</VehicleMode>
    <PublishedLineName>53</PublishedLineName>
    <DirectionName>Wootton</DirectionName>
    <OperatorRef>bed-5100</OperatorRef>
    <MonitoredCall>
      <AimedDepartureTime>2013-03-21T12:49:00.000Z</AimedDepartureTime>
    </MonitoredCall>
  </MonitoredVehicleJourney>
</MonitoredStopVisit>
</StopMonitoringDelivery>
</ServiceDelivery>
</Siri>

リストボックスに ExpectedDeperatureTime のみを表示するのが好きです。

私は次のコードでこれをやろうとしました:

string xmlFormat = responseFromServer;

            XDocument doc = XDocument.Parse(xmlFormat);

            var list = doc.Root.Elements("ExpectedDepartureTime")
                          .Select(element => element.Value);


            foreach (string i in list)
            {
                lstHours.Items.Add(i);
            }

別のスレッドでこのコードを見つけました: How can I transform XML into a List<string> or String[]?

スレッドで「id」を使用して例に従うと、機能しますが、xmldata では機能しません。理由はわかりません...

大きすぎますか?私は何かを忘れていますか?

ありがとう!

4

1 に答える 1

0

This will get a list of departure times:

XNamespace ns = "http://www.siri.org.uk/";

List<DateTime> departureTimes = doc.Descendants(ns + "ServiceDelivery")
   .Elements(ns + "StopMonitoringDelivery")
   .Elements(ns + "MonitoredStopVisit")
   .Elements(ns + "MonitoredVehicleJourney")
   .Elements(ns + "MonitoredCall")
   .Elements(ns + "ExpectedDepartureTime")
   .Select(x => (DateTime)x)
   .ToList();

or as strings

List<string> departureTimes2 = 
    departureTimes.Select( dt => dt.ToString() ).ToList();
于 2013-03-21T12:24:56.960 に答える