2
votes

I added a Silverlight application to my ASP.NET website. Visual Studio made a new silverlight project and added its xap to the ClientBin folder under the project of my website. So both the projects are under one solution.

My Silverlight app is supposed to read an xml file and I was unable to make it access the file from the client bin folder under the website project. Adding a reference to that project does not work since it says only references to other silverlight applications can be added. Right now its working when the file is under the silverlight project but not when it is under the website project.

how can I make it read the file from website project?

The project structure is

WEBSITE1 (solution)
 -WEBSITE1 (project)
  -ClientBin
   -file0.xml
   -silverlightchart.xap
 -SilverlightChart
   -file1.xml

I can access file1.xml using

XDocument document = XDocument.Load("file1.xml");

I want to access file0.xml but no path works for me, for e.g,

XDocument document = XDocument.Load("~/ClientBin/file0.xml");

and WEBSITE1 is the startup project

1
If my asnswer below hasn't helped please post the code you are using to access the xml file.AnthonyWJones

1 Answers

0
votes

You should be able to read a file from the ClientBin folder simply enough without needing to do anything special. At a guess I would say the you have accidentally set the Silverlight application as the startup project. In this scenario you want the website to be the startup project then either have the Silverlight apps test page marked as the start page or to navigate to the silverlight page once the browser has started.

Edit

The problem you have is that the Load method is synchronous but Silverlight does not support synchronous access to web resources. Hence passing a Uri to the Load method will only work if the that Uri can be fulfilled by content in the Xap. Thats why an Xml file in the silverlight project works because it ends up in the Xap.

To fetch Xml from the site you need to do this:-

 WebClient client = new WebClient();
 client.DownloadStringCompleted += (s, args) =>
 {
     XDocument document = XDocument.Load(args.result);
     SomeFunctionToContinueWithDocumentProcess(document);
 }
 client.DownloadStringAsync(new Uri("file0.xml", UriKind.Relative);
 // code exits here but _document won't be loaded yet