XML 下载可以在模拟器中使用,但不能在手机上使用

发布于 2024-12-17 20:05:10 字数 1940 浏览 0 评论 0原文

我正在从我创建的站点下载 XML 文件,它在模拟器上运行良好;但是,它在手机上根本不起作用。返回时会出现 Web 异常错误和 IO 错误...并且 HttpsCompleted 事件的 error 属性表示错误,远程服务器返回了错误。未找到文件。但这适用于我的模拟器。

    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>

I am downloading an XML file from a site I created and it works fine on the emulator; however, it doesn't work at all on the phone. It comes back with a web exception error and an IO error...and the error property from the HttpsCompleted event says error the remote server returned an error. File not Found. BUT this works on my emulator.

    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 Looks like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2024-12-24 20:05:10

首先,应该禁止这样的代码,因为您面临 NullReferenceException 的风险:

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

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

First, Code like this should be prohibited because you risk the NullReferenceException :

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

Then, emulator uses internet connection of your computer, so if you use the same connection with your phone the problem is related to your phone, not related to your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文