Windows phone 7 使用 XDocument 读取 XML [英] Windows phone 7 reading XML with XDocument

查看:27
本文介绍了Windows phone 7 使用 XDocument 读取 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作我的第一个 Windows Phone 7 应用程序.它涉及向服务器查询航班信息.然后接收一个 XML 文档.然后,我想根据返回的 XML 创建一系列对象.但是存在一个问题,因为对象值为空.

Hi im trying to make my first windows phone 7 app. It involves querying a server about flight information. Then receiving a XML document. I then want to create a series of objects based on the XML I get back. However there is a problem since the object values are blank.

我的代码

    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 的开头,整个事情有数百行长,所以不会但它全部结束.我想要的飞行标签埋在那里.

and the section of XML I want looks like this. This is also the first use of the Flight tag in the XML. Added the start of the XML whole thing is hundreds of lines long so won't but it all up. The Flight Tag I want is burred down in there.

<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>

我可能犯了一些简单的错误.所有帮助表示赞赏.谢谢.编辑我现在收到一个方法传递错误.为简单起见,我将日期时间更改为字符串

I've probably made some simple mistake. All help appreciated. Thanks. edit im now getting a method passing error. I changed the datetime to string for simplicity

推荐答案

我猜你没有向我们展示完整的 XML,它可能指定了一个默认的命名空间.命名空间看起来像 xmlns="http://www.domain.com".如果确实如此,那么您的 LINQ to XML 查询需要引用该命名空间,这可以按如下方式完成:

My guess is you're not showing us the complete XML and it likely has a default namespace specified. A namespace will look like xmlns="http://www.domain.com". If this is indeed the case then your LINQ to XML queries need to reference that namespace, and this can be done as follows:

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 +.

Notice that you need to prefix each element name with the namespace, hence the use of ns + throughout the code.

这篇关于Windows phone 7 使用 XDocument 读取 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆