我正在试图制作一个应用程序,显示包括公交车号码,Aimed小时和预计时间在内的bush application。如果在LINQ中声明(如果没有值做某事......)
我从HttpWebRequest
得到我的(活的)信息。我的请求响应以XML格式存储在字符串变量中。
我可以得到所有我想要的信息;如公交小时,Aimed小时和预计小时。
问题是,如果没有预期的小时,将不会显示任何内容。我喜欢在没有预期的时间我的代码只是需要相同的值针对时间:
一个例子
Bus | Aimed | Execepted
-----------------------
1 | 17:05 | 17:07
2 | 17:05 | <nothing> so take value of aimed -> 17:05
我已经下面的代码
//XMLResponse put in documentRoot
//responseFromServer is a string variable in XML format with all the information
XElement documentRoot = XDocument.Parse(responseFromServer).Root;
XNamespace ns = "http://www.siri.org.uk/";
var buses = (from tblBuses in documentRoot.Descendants(ns + "PublishedLineName")
select tblBuses.Value).ToList();
var expHours = (from tblHours in documentRoot.Descendants(ns + "ExpectedDepartureTime")
select tblHours.Value).ToList();
foreach (var bus in buses)
{
string output = bus.Substring(bus.IndexOf('T') + 1);
int index = output.IndexOf(".");
if (index > 0)
output = output.Substring(0, index);
listBox1.Items.Add("Bus: " + output);
}
//Show every ExpectedDepartureTime
//If there is no expectedTime take value AimedDepartureTime
foreach (var expH in expHours)
{
string output = expH.Substring(expH.IndexOf('T') + 1);
int index = output.IndexOf(".");
if (index > 0)
output = output.Substring(0, index);
lstHours.Items.Add(output);
}
为是更清楚了解我的XML响应,下面是我的XML响应示例(其中一个带有AimedDeparturetime和Expected,另一个带有预期的)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Siri version="1.0" xmlns="http://www.siri.org.uk/">
<ServiceDelivery>
<ResponseTimestamp>2013-03-26T16:09:48.181Z</ResponseTimestamp>
<StopMonitoringDelivery version="1.0">
<ResponseTimestamp>2013-03-26T16:09:48.181Z</ResponseTimestamp>
<RequestMessageRef>12345</RequestMessageRef>
<MonitoredStopVisit>
<RecordedAtTime>2013-03-26T16:09:48.181Z</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-26T16:11:00.000Z</AimedDepartureTime>
<ExpectedDepartureTime>2013-03-26T16:11:28.000Z</ExpectedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
---------------------------------------------------
<MonitoredStopVisit>
<RecordedAtTime>2013-03-26T16:09:48.181Z</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-26T16:19:00.000Z</AimedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
</StopMonitoringDelivery>
</ServiceDelivery>
</Siri>
因此,目前我的应用程序不显示公交车的每个出发时间。
我该如何解决这个问题?
谢谢!
re'SingleOrDefault()。Value' - 因此,您应该将'XElement'强制转换为'string'而不是使用'Value'。 – 2013-03-26 20:05:38
这也是一个选择;它会使代码更清晰一些,但不一定更清晰。小调挑剔:它不只是一个演员,而是一个转换。 – Thorarin 2013-03-26 21:34:53
非常感谢!这对我有用! – 2013-03-27 09:30:00