Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight Windows phone 7使用XDocument读取XML_Silverlight_Windows Phone 7 - Fatal编程技术网

Silverlight Windows phone 7使用XDocument读取XML

Silverlight Windows phone 7使用XDocument读取XML,silverlight,windows-phone-7,Silverlight,Windows Phone 7,嗨,我正在尝试制作我的第一个windows phone 7应用程序。它涉及到向服务器查询航班信息。然后接收一个XML文档。然后,我想基于我得到的XML创建一系列对象。但是,存在一个问题,因为对象值为空 我的代码 private void SearchButton_Click(object sender, RoutedEventArgs e) { getResults("http://test.com/"); } public void ge

嗨,我正在尝试制作我的第一个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>
我可能犯了一些简单的错误。 谢谢你的帮助。 谢谢
编辑im现在得到一个方法传递错误。为了简单起见,我将datetime改为string

我猜您没有向我们展示完整的XML,它可能指定了默认名称空间。名称空间将类似于xmlns=http://www.domain.com. 如果确实是这种情况,那么LINQ到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+。

谢谢Ahmad,您说得对,指定了名称空间。然而,我做了改变,仍然有相同的problem@user704314您能用所做的更改更新您的问题并用名称空间显示更多的XML吗?为了简单起见,我已经更新了代码并更改了datetime参数,因为现在我收到一个错误,说在创建新航班时FormatException未经处理。@user704314可能是其他原因导致了异常,可能是在飞行类中,或者是在使用它的线路上。为了验证,我建议在查询之后放置一个断点并检查结果。如果它在到达断点之前失败,那么作为查询的一部分,很可能会出现一些问题。事实上,您可以注释掉Flight并临时使用匿名类型来验证查询是否按如下方式工作:选择new//Flight。通过从飞行中删除注释并检查代码,这些都应该起作用并揭示问题所在。