通用 Windows 应用程序,无法执行 doc.LoadXml(string)

Universal Windows App, cant execute doc.LoadXml(string)

我正在尝试使用以下代码块通过通用 windows 应用检索 WOIED-

    string woeID;
    private string GetWOEID(string zipCode)
    {
        woeID = "";

        XmlDocument woeidData = new XmlDocument();
        string query = String.Format("http://where.yahooapis.com/v1/places.q('{0}')?appid={1}", zipCode, YahooAPI_ID);

        try
        {
            woeidData.LoadXml(query);

        }
        catch (Exception)
        {
        }

        XmlNodeList kk = woeidData.GetElementsByTagName("woeid");
        if (kk.Count != 0)
        {
            woeID = kk[0].InnerText;
            textBox.Text = "";
            GetWeather(); // Calling getweather method.
            return woeID;


        }
        else
        {
            woeID = "";
            textBox.Text = "";
            return woeID;

        }

    }

其中 zipCode 来自 textBox 输入,YahooAPI_ID 是我的开发者密钥。我需要先检索 WOEID 以将其传递给 GetWeather() 方法以从 yahoo 获取 xml 中的天气预报。但问题是在 try{woeidData.LoadXml(query);} 之后它总是在调试器中进入 catch (exception){}。虽然此代码块在使用代码 try {woeidData.Load(query)}

之前适用于 winform 应用程序

任何指出我做错的地方都将不胜感激!

XmlDocument.LoadXml 方法需要一个实际的 xml、https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx 字符串,而不是指向资源的 uri,您需要单独获取 http 调用的内容.

例如,您可以使用 XmlDocument.Load 方法,该方法采用流并使用 HttpWebRequest https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx 及其 GetResponse().GetResponseStream() 方法加载 xml。

好吧,我就是这样做的,以防万一有人想知道并偶然发现这个...

private async void button_Click(object sender, RoutedEventArgs e)

{
string url = String.Format("http://url");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); 
            StreamReader reader = new StreamReader(response.GetResponseStream());
            XmlDocument elementdData = new XmlDocument();
            woeidData.Load(reader);
            XmlNodeList element = woeidData.GetElementsByTagName("elementID");
}