嗨,我试图制作我的第一个Windows Phone 7应用程序。它涉及向服务器查询航班信息。然后接收XML文档。然后我想根据我得到的XML创建一系列对象。但是由于对象值为空白,因此存在问题。
我的代码
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
getResults("http://test.com/");
}
public void getResults(string websiteURL)
{
WebClient c = new WebClient();
c.DownloadStringAsync(new Uri(websiteURL));
c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
}
void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
lock (this)
{
string s = e.Result;
XmlReader r = XmlReader.Create(new MemoryStream(System.Text.UnicodeEncoding.Unicode.GetBytes(s)));
// So something with the XML we get back
XDocument data = XDocument.Load(r);
var ns = data.Root.GetDefaultNamespace();
var flights = from query in data.Descendants(ns+"Flight")
select new Flight
{
AircraftType = (int)query.Element(ns + "AircraftType"),
ArrivalTerminal = (int)query.Element(ns + "ArrivalTerminal"),
Carrier = (string)query.Element(ns + "Carrier"),
DepartureTerminal = (int)query.Element(ns + "DepartureTerminal"),
Duration = (string)query.Element(ns + "Duration"),
EndDateTime = (string)query.Element(ns + "EndDateTime"),
EndPoint = (string)query.Element(ns + "EndPoint"),
FlightIndexNo = (int)query.Element(ns + "FlightIndexNo"),
FlightNo = (int)query.Element(ns + "FlightNo"),
NumStops = (int)query.Element(ns + "NumStops"),
OperatedBy = (string)query.Element(ns + "OperatedBy"),
StartDateTime = (string)query.Element(ns + "StartDateTime"),
StartPoint = (string)query.Element(ns + "StartPoint")
};
//checking if anything is there.
string result ="";
foreach (Flight i in flights)
{
result += i.Carrier;
}
resultsBlock.Text = result;
}
}
public class Flight
{
public int aircraftType;
public int arrivalTerminal;
public string carrier;
public int departureTerminal;
public string duration;
public string endDateTime;
public string endPoint;
public int flightIndexNo;
public int flightNo;
public int numStops;
public string operatedBy;
public string startDateTime;
public string startPoint;
//Getter and setters
我想要的XML部分看起来像这样。这也是XML中第一次使用Flight标签。添加了XML整个事情的开头是数百行,所以不会,但一切都结束了。我想要的飞行标签在那里被烧毁。
<FindFlightsResponse xmlns="urn:webjet.com.au" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayMessage i:nil="true" />
<OutboundFlightInfo>
...
<Flight>
<AircraftType>734</AircraftType>
<ArrivalTerminal>3</ArrivalTerminal>
<Carrier>QF</Carrier>
<DepartureTerminal>1</DepartureTerminal>
<Duration>PT1H25M</Duration>
<EndDateTime>2011-04-20T07:25:00</EndDateTime>
<EndPoint>SYD</EndPoint>
<FlightIndexNo>1</FlightIndexNo>
<FlightNo>400</FlightNo>
<NumStops>0</NumStops>
<OperatedBy>QF</OperatedBy>
<StartDateTime>2011-04-20T06:00:00</StartDateTime>
<StartPoint>MEL</StartPoint>
</Flight>
我可能犯了一些简单的错误。 所有帮助赞赏。 谢谢。 编辑即时获取方法传递错误。为简单起见,我将datetime更改为字符串
答案 0 :(得分:0)
我的猜测是你没有向我们展示完整的XML,它可能有一个默认的命名空间。命名空间看起来像xmlns="http://www.domain.com"
。如果确实如此,则LINQ to XML查询需要引用该命名空间,这可以通过以下方式完成:
var ns = data.Root.GetDefaultNamespace();
var flights = from query in data.Descendants(ns + "Flight")
select new Flight
{
AircraftType = (int)query.Element(ns + "AircraftType"),
ArrivalTerminal = (int)query.Element(ns + "ArrivalTerminal"),
Carrier = (string)query.Element(ns + "Carrier"),
DepartureTerminal = (int)query.Element(ns + "DepartureTerminal"),
Duration = (string)query.Element(ns + "Duration"),
EndDateTime = (DateTime)query.Element(ns + "EndDateTime"),
EndPoint = (string)query.Element(ns + "EndPoint"),
FlightIndexNo = (int)query.Element(ns + "FlightIndexNo"),
FlightNo = (int)query.Element(ns + "FlightNo"),
NumStops = (int)query.Element(ns + "NumStops"),
OperatedBy = (string)query.Element(ns + "OperatedBy"),
StartDateTime = (DateTime)query.Element(ns + "StartDateTime"),
StartPoint = (string)query.Element(ns + "StartPoint")
};
请注意,您需要在每个元素名称前加上命名空间,因此在整个代码中使用ns +
。