XML下载适用于模拟器,但不适用于手机

时间:2011-11-24 13:06:42

标签: c# xml windows-phone-7 webclient

我从我创建的网站下载XML文件,它在模拟器上工作正常;但是,它在手机上根本不起作用。它返回Web异常错误和IO错误... HttpsCompleted事件的错误属性表示远程服务器返回错误的错误。文件未找到。但这适用于我的模拟器。

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri("http://.../SessionInfo.xml"));

    }

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        XDocument doc = null;
        string results = null;

        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
            List<XElement> xelem = xdoc.Root.Elements() as List<XElement>;

            results = e.Result;

            var sessions = from x in xdoc.Descendants("Session")
                           select new
                           {
                               ID = x.Descendants("ID").First().Value,
                               TITLE = x.Descendants("Title").First().Value,
                               TIME = x.Descendants("Time").First().Value,
                               DESCRIPTION = x.Descendants("Description").First().Value
                           };

            foreach (var wd in sessions)
            {
                sessionsList.Add(new Session(wd.ID, wd.TITLE, wd.TIME, wd.DESCRIPTION));
                Debug.WriteLine("Session ID is {0}, Title is {1}, Time is {2}", wd.ID, wd.TITLE, wd.TIME);
            }
        }

        SessionInfoList.ItemsSource = sessionsList;

XML看起来像:

<request><Session><ID>1234-1234-1234-1234</ID><Title>Session Title</Title><Time>10:00AM-11:30AM</Time><Description>Some description.</Description></Session></request>

1 个答案:

答案 0 :(得分:0)

首先,应该禁止这样的代码,因为冒着NullReferenceException的风险:

TITLE = x.Descendants("Title").First().Value

然后,模拟器使用您的计算机的互联网连接,因此如果您使用与手机相同的连接,则问题与您的手机有关,与您的代码无关。