开发者

Silverlight Cannot find XML data source

I am very new to Silverlight development. I understand that this is client side technology therefore the paradyme is differant from that of conventional ASP.NET development. Having said that, I don't understand where my server side code is deployed.

I have a silver light \ MVC application. I am trying to read an XML document from within my 'Models' folder. The following peice of code is executed from within a class that is in the same location as the XML document, 'Models'. The load() results in a SystemIOFileNotFound exception. I noticed that when building the application the XML document is not laid down in the same location as the web project's assembly. I assume this is specific to the fact that this is a Silverlight project. Can someone tell me what I'm missing?

  _xdoc = new XDocument();
   _xdoc = XDocument.Load(new Uri("videos.xml",U开发者_JS百科riKind.Relative).ToString());

Edit..

The behavior I am after is the start page (silverlight) populates controls via a server side controller. ie localhost/video


Silverlight can't access your filesystem (thankfully), which is why you can't access the file. Try embedding it as a resource, or storing it in the local storage API provided by silverlight.


Assuming that your Models folder is in the Web project (i.e. not the Silverlight project), I think that your problem is unrelated to Silverlight. The code loading the XML file assumes that the file is in the current directory, so you need to ensure this through your deployment technique.

If you are doing this in the Silverlight part, you should put the XML file in an embedded resource and access it as a stream (get it with Assembly.GetManifestResourceStream) or as a resource (a la WPF, not an embedded resource) and access it with the package part syntax.


The problem was that I was attempting to access this static resource as you would in typical ASP.net. However I found it necessary to map the path to the file using the current HTTPContext:

HttpContext.Current.Server.MapPath("~/App_Data/videos.xml");

So the above worked for me. Since this code is in the web project and not in the silverlight project I am still unclear as to why I cannot just access this resource using a relative path. This code will be executed in the context of the web server. i.e.

XDocument.load(../App_Data/videos.xml);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜