2

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?

apaderno
  • 28,547
  • 16
  • 75
  • 90
C_Rance
  • 661
  • 12
  • 25
  • Hmm... if you need to change it on the client, and upload it back to the server - couldn't you just put your config settings into a database table instead of a file, and update those settings on the server?? – marc_s Aug 07 '10 at 07:18
  • thanks, but these settings for my config will include database connection to the database server which may be hosted in another com. Some times, there are rules like no hosting of my own database file/database server. Therefore the settings will need to be in an external file – C_Rance Aug 07 '10 at 07:23
  • @C_Rance: You could use a "dictionary way" to do it. On the server you will perform the file editing, on the SL client you just send an update command like `UpdateConfiguration("FontSize", 45615);`. Which should be able to take in an array of value pairs as well. Reuploading the whole file just might be easier though. The bonus for the "dictioanary way" is that you could easily implement validation of updates individually instead of validating through xsd. – Jaroslav Jandek Aug 07 '10 at 08:30
  • @Jaroslav Jandek thanks for your idea, but do you have some examples on it? I roughly understand about the dictionary, but I cant figure out how u pass the command. and how to setting get updated? By stating the word command, I believe I have to use a service(let service update the setting), or use a socket. DO guide me,thanks – C_Rance Aug 07 '10 at 09:59
  • @C_Rance: Yes, I would use a service. Have a look at the WCF tutorials: http://msdn.microsoft.com/en-gb/netframework/first-steps-with-wcf.aspx or create a new web service project - pretty straightforward. – Jaroslav Jandek Aug 08 '10 at 08:18
  • @Jaroslav thanks, I found a workaround too,using generic handler – C_Rance Aug 08 '10 at 22:09

1 Answers1

1

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
  • 661
  • 12
  • 25