如何在OOB应用程序中使用web客户端方法从服务器获取xml文件

本文关键字:方法 客户端 服务器 获取 文件 xml web OOB 应用程序 | 更新日期: 2023-09-27 18:26:20

我在silverlight中使用浏览器外应用程序。

我在使用下面提到的代码加载xml文件时遇到问题。

string contentUri = Application.Current.Host.Source
            .AbsoluteUri;
        var contentUri1 = contentUri.Substring(0, contentUri.LastIndexOf("/")) + "/Hello1.xml";
        WebClient wc = new WebClient();
        wc.OpenReadCompleted+=(open,read)=>
        {

            string content;
            using (StreamReader reader = new StreamReader(read.Result,Encoding.Unicode))
            {
                byte[] m_Bytes = ReadToEnd(read.Result);
                string s = Encoding.UTF8.GetString(m_Bytes, 0, m_Bytes.Length);
            }
        };
        wc.DownloadProgressChanged
            += (chang,dh)=>
          {
            };
        wc.OpenReadAsync(new Uri(contentUri));

我的xml文件中包含

<Root>
<element>FirstElement</element>
</Root>

我得到了垃圾值作为输出。有人能帮助我如何下载原始xml内容吗?

如何在OOB应用程序中使用web客户端方法从服务器获取xml文件

当您在浏览器外模式下调用web客户端调用时,它假设您已经创建了RootVisual,因为web客户端显然将在其Dispatcher上运行。否则,您将不会得到服务器的响应,而且奇怪的是,甚至不会抛出异常!!

无论如何,Jeremy的这篇文章解释了细节:

http://csharperimage.jeremylikness.com/2010/05/webclient-and-deploymentcatalog-gotchas.html