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);
            }

我在另一个线程上找到了这段代码:如何将 XML 转换为 List<string> 或 String[]?

当我在线程上使用带有“id”的示例时,它可以工作,但不适用于我的 xmldata,我不知道为什么......

有这么大吗?我忘记了什么吗?

谢谢!

4

1 回答 1

0

这将获得出发时间列表:

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();

或作为字符串

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