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

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

最佳答案

首先,像这样的代码应该被禁止,因为你有 NullReferenceException 的风险:

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

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

关于c# - XML 下载适用于模拟器,但不适用于手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257568/

相关文章:

c# - 如何使用 Microsoft.Office.Interop.Word 将光标移动到特定段落?

即使在设计模式下,带计时器的 C# 用户控件也会启动

xml - Scala XML 支持如何工作?

c# - Windows Phone 7 中的可缩放图像

c# - Windows 身份验证局域网和广域网

c# - 求解器基础单纯形求解需要大量时间

java - 在Android中显示文本后面的多种形状

xml - 根据属性值限制元素出现的次数

c# - Windows Phone 7,收藏全景项目

windows-phone-7 - WP 8 : How to deploy/export Application from Visual Studio