将XML转换为字符串并在列表框中显示特定元素

时间:2013-03-21 12:00:39

标签: c# xml-parsing linq-to-xml

我正在开发一个程序,我在做一个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我不知道为什么......

它变大吗? 我忘了什么吗?

谢谢!

1 个答案:

答案 0 :(得分: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();