Metro 应用程序如何读取 XML API?

Metro Application How to Read XML API?(Metro 应用程序如何读取 XML API?)

本文介绍了Metro 应用程序如何读取 XML API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在尝试学习如何使用 XAML 以及如何使用 Visual Studio 11 Developer Preview 构建新的 Windows Metro 应用程序.

Ok so I am trying to learn how to work with XAML and how to build new windows metro applications using Visual Studio 11 Developer Preview.

虽然我不知道如何像使用 C# 一样读取 XML 文件,但我遇到了一个问题.例如,这是我过去的做法.

I have a problem though I don't know how to read XML files the same way I use to using C#. For example here is how I did it in the past.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            string UrlString = "http://v1.sidebuy.com/api/get/73d296a50d3b824ca08a8b27168f3b85/?city=nashville&format=xml";
            XmlTextReader reader = new XmlTextReader(UrlString);
            XmlNodeType type;

            while (reader.Read())
            {
                type = reader.NodeType;

                if ((type == XmlNodeType.Element) && (reader.Name == "highlights"))
                    {
                        reader.Read();
                        if (reader.Value != "" && reader.Value != null)
                        {
                            listBox1.Items.Add(reader.Value);
                        }
                    }
                }
        }

但这在我的 Metro 应用程序中不起作用.我需要知道如何为地铁做到这一点.显然 XmlTextReader 不再有效.有什么代码或建议吗?

But this won't work in my metro application. I need to know how to do this for metro. Apparently XmlTextReader is no longer valid. Any code or suggestions?

谢谢

推荐答案

你可以使用 XmlDocument.LoadFromUriAsync.这也应该使您的代码更简单.

You can use XmlDocument.LoadFromUriAsync. This should also make your code a lot simpler.

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string UrlString = "http://v1.sidebuy.com/api/get/73d296a50d3b824ca08a8b27168f3b85/?city=nashville&format=xml";
            var xmlDocument = await XmlDocument.LoadFromUriAsync(UrlString);

            //read from xmlDocument for your values.
         }

根据注释修复代码.

这篇关于Metro 应用程序如何读取 XML API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Metro 应用程序如何读取 XML API?

基础教程推荐