C# Silverlight,从外部应用程序包打开xml

C# Silverlight,从外部应用程序包打开xml,c#,silverlight,C#,Silverlight,我必须在Silverlight应用程序中打开并读取xml文件。我无法将此文件放入资源(我的老师的遗嘱)。我尝试使用以下代码: XmlReader reader = XmlReader.Create("products.xml"); 但有一个错误: Cannot find file 'products.xml' in the application xap package. 此xml文件当前位于.xap旁边的调试文件夹中 我该怎么做才能让它工作 XML文件(以防万一): 首先需要WebCli

我必须在Silverlight应用程序中打开并读取xml文件。我无法将此文件放入资源(我的老师的遗嘱)。我尝试使用以下代码:

XmlReader reader = XmlReader.Create("products.xml");
但有一个错误:

Cannot find file 'products.xml' in the application xap package.
此xml文件当前位于.xap旁边的调试文件夹中

我该怎么做才能让它工作

XML文件(以防万一):


首先需要
WebClient
的帮助才能从web服务器下载xml文件。下面是一个实用的帮助方法

 public static void GetStreamFromUri(Uri uri, Action<Stream> returnResult)
 {
      WebClient client = new WebClient();
      client.OpenReadCompleted += (s, args) =>
      {
           returnResult(args.Result);
      };
      client.OpenReadAsync(uri);
 }

到目前为止,您编写的代码可能是同步工作的。Silverlight允许同步加载和处理XAP中找到的资源。然而,网络操作总是异步的。我猜“(我的老师的意愿)”实际上是您学习了Silverlight编码的这个重要的异步方面,以及它对您需要编码的方式有什么影响。

您的代码中有3个小错误。我已经更正了它们,并尝试运行您的代码(没有进一步的修改)。不幸的是,returnResult(args.Result)中存在异常:“操作过程中发生异常,导致结果无效。请检查InnerException以了解异常详细信息。”我将xml文件放在问题中以防万一。@gisek:我发现了两个与OpenReadCompleted相关的异常,但我看不到第三个异常?什么是内在的例外?在OpenReadCompleted中放置一个
try..catch
,并在catch-like-MessageBox.Show(err.ToString())中执行一些愚蠢的操作,为您提供一个断点。您正在使用Web服务器主机吗?“products.xml”应该与Xap一起出现在web项目的ClientBin文件夹中,是这样吗?第三个错误:GetStreamFromUri(Uri url我没有使用Web服务器主机。我在那里看到,您必须在ASP的不同选项之间进行选择,而我应该只将silverlight对象放在我的php网站上。虽然我可能对这里的理解不好。@gisek:所以,如果我明白了这一点,您只允许将silverlight XAP放在php网站上,但Products.xml不在XAP中?因此,如果它不在XAP中,也不在网站上,它还能在哪里?如果这是家庭作业,您应该在其上有#家庭作业标记正常Silverlight仅支持读取隔离存储中的文件或使用OpenFileDialog
System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details. ---> System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
   in System.Net.WebRequest.Create(Uri requestUri)
   in System.Net.WebClient.GetWebRequest(Uri address)
   in System.Net.WebClient.OpenReadAsync(Uri address, Object userToken)
   --- The end of stack trace of inner exceptions (my translation) ---
   in System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   in System.Net.OpenReadCompletedEventArgs.get_Result()
   in ProjektAI.MainPage.<>c__DisplayClass1.<GetStreamFromUri>b__0(Object s, OpenReadCompletedEventArgs args)
 public static void GetStreamFromUri(Uri uri, Action<Stream> returnResult)
 {
      WebClient client = new WebClient();
      client.OpenReadCompleted += (s, args) =>
      {
           returnResult(args.Result);
      };
      client.OpenReadAsync(uri);
 }
GetStreamFromUri(new Uri("products.xml", UriKind.Relative), stream =>
{
     using (stream)
     {
         XmlReader reader = XmlReader.Create(stream);

         // Rest of reader processing code called here.
     }
});

//  Note really really well: **Code here will run before XmlReader processing**