tags:

views:

21

answers:

1

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

A: 

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
AnthonyWJones
I use XDocument document = XDocument.Load("chart.xml");when the chart.xml file exists in silverlight project. What should the path be for accessing the file from the other projects clientbin? thanks
anon2
@anon2: See my edit
AnthonyWJones
Thanks Anthony, right now another solution seems to be working, if it fails I will try it the way you have mentioned. I added the file0.xml to SilverlightChart as a link(Add existing item > drop down on add button "add as link"). On changing project folders it seems the link is dynamically updated so theoretically this should work
anon2
@anon2: Glad you've got it working, often the whole point of putting an xml file outside of the silverlight project is so that it can be edited in-situ without there being a need to re-build the Xap.
AnthonyWJones