Retrieve XML from https using WebClient/HttpWebRequest - WP7

前端 未结 1 575
囚心锁ツ
囚心锁ツ 2020-12-06 14:15

I\'m trying to retrieve an XML document from a server and store it locally as a string. In desktop .Net I didn\'t need to, I just did:

        string xmlFile         


        
相关标签:
1条回答
  • 2020-12-06 14:56

    I rather think you've overcomplicated things. Below is a very simple example which requests an XML document from a URI over HTTPS.

    It downloads the XML asynchronously as a string and then uses XDocument.Parse() to load it.

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += HttpsCompleted;
            wc.DownloadStringAsync(new Uri("https://domain/path/file.xml"));
        }
    
        private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
    
                this.textBox1.Text = xdoc.FirstNode.ToString();
            }
        }
    
    0 讨论(0)
提交回复
热议问题