【问题标题】:Windows phone 7 reading XML with XDocumentWindows phone 7 使用 XDocument 读取 XML
【发布时间】:2011-08-04 00:17:30
【问题描述】:

您好,我正在尝试制作我的第一个 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>

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

【问题讨论】:

    标签: silverlight windows-phone-7


    【解决方案1】:

    我的猜测是您没有向我们展示完整的 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 +

    【讨论】:

    • 感谢 Ahmad,您说得对,指定了一个命名空间。但是我做了更改,仍然有同样的问题
    • @user704314 你能用所做的更改更新你的问题,并用命名空间显示更多的 XML 吗?
    • 为了简单起见,我更新了代码并更改了 datetime 参数,因为现在我收到一条错误消息,提示我在创建新航班时未处理 FormatException。
    • @user704314 我认为其他原因可能导致异常,可能在 Flight 类或使用它的其他东西中。为了验证我建议在查询之后放置一个断点并检查flights 结果。如果它在到达断点之前失败,那么作为查询的一部分可能会出现问题。实际上,您可以注释掉Flight 并临时使用匿名类型来验证查询是否像这样工作:select new //Flight。然后从Flight 中删除 cmets 并检查代码,这应该可以解决问题并揭示问题所在。
    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多