I have an application that reads an XML file for information on projects and displays them in a timelines. The user has the ability to modify and add projects, so I want to save this XML file.
I have a Silverlight application that displays the data, and a web project that hosts the XML file in it's ClientBin folder. The application gets the XML file by using the WebClient class:
WebClient dataSource = new WebClient();
dataSource.OpenReadCompleted += dataSource_OpenReadCompleted;
dataSource.OpenReadAsync(new Uri("ProjectData.xml", UriKind.Relative));
Then in the dataSource_OpenReadCompleted method it gets开发者_Go百科 the stream from the e.Result object and reads it into an XDocument object which I parse using LINQ. This works fine.
Now I want to save my changes back to the web project. I have the modified XML in an XDocument object ready to go ... and I'm not sure how to write back.
There is a WebClient.OpenWriteAsync method, but I'm not sure how to use it. Googling doesn't give any clear results.
Thanks,
Andrew
Silverlight code runs on the client so...
You could try making a webservice that accepts the xml data and a file name. Then have that web service write the file back to the file system. Also please ensure that you use locks on the file so that multiple users don't try to write to the file at the same time.
Thanks for the suggestion kurtnelle, that would also work. I figure I'll write what my solution was for anyone who stumbles upon this question.
One cannot use OpenWriteAsync the same way you use OpenReadAsync because for security reasons Silverlight cannot write directly to the file system.
I ended up using an HTTPHandler method in the web project. I created a file called XMLHandler.ashx that listened for a call to the webclient. In the Silverlight app I called webclient.UploadStringAsync with the URI of the web project and the xml file as the data string. When the HTTPHandler hears this, it saves it to the client bin. It works perfectly!
精彩评论