views:

87

answers:

1

I am currently using xml as a config file for my silverlight application. I have no problem reading the file, but now that I also require to update the file through web(preferably silverlight as will show preview of font colors, size etc), the only method I thought of is to generate the whole file and overwrite the existing through uploading.

Is there an easier way to do so?

A: 

Well, found my own answer, sharing here too.

When I read the xml file, I'm using linq's for xml. So there is an option for doc.save() So by doing this:

System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(s);

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sr = new System.IO.StringWriter(sb);
doc.Save(sr);
string ss = sb.ToString();//result
sr.Close();

I have got a saved xml file in ss. Then using web client's OpenWriteCompleted,I used

Stream outputStream = e.Result;
byte[] fileContent = Encoding.UTF8.GetBytes(ss);
outputStream.Write(fileContent, 0, fileContent.Length);
outputStream.Close();

Using web client's OpenWriteAsync, the URI will be the uri of my generic handler. Inside the generic handler

FileStream fs = File.Open(context.Server.MapPath("~/ClientBin/" + "test.txt"), FileMode.Create);

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
{
   fs.Write(buffer, 0, bytesRead);
}
fs.Close();

credit to the author ( Nipun Tomar) @ as most of the ideas came from his site

http://www.c-sharpcorner.com/UploadFile/nipuntomar/FileUploadsilverlight03182009030537AM/FileUploadsilverlight.aspx

C_Rance