tags:

views:

46

answers:

3

I have a web form, that needs to save the data that the user enters, into a file and on the client pc, that will also be able to read from the saved file and repopulate the fields at a later time. No files will be saved to the server side, so I expect streaming needs to be involved at writing time.

I decided XML would be an easy way to do this, but I'm stymied on methodology. XML documents? XML Writers? I'm stumped on the right search terms to even get what I want.

Thanks in advance for any pointers.

A: 

Sounds like you need to store the data in a cookie. You can't write from the browser to a file on the client pc. The browser operates within a sandbox, which protects the client pc from malicious websites.

Take a look at this page on saving and retrieving cookie data to/from a client machine:

http://www.aspnettutorials.com/tutorials/network/cookies-csharp.aspx

Daniel Dyson
Unless you're using ActiveX objects ... which I think is what snow and jrummell are doing, right? But then it only works in IE.
LarsH
@LarsH True, only really useful on intranet if you know all of your users only use IE.
Daniel Dyson
Just to clarify the context of my comment ... I meant "You can't write from the browser to a file on the client pc" unless you're using ActiveX objects, which I think is what snow and ... etc.
LarsH
Yes, that is how I understood your comment
Daniel Dyson
+1  A: 

You'll want to use XML serialization. Take a look at this MSDN article. Here's an excerpt on serialization and deserialization:

How to Serialize an Object

To Serialize and object, we need few instances of the in-built classes. So lets first create an instance of a XmlDocument class from System.Xml namespace. Then create an instance of XmlSerializer class from System.Xml.Serialization namespace with parameter as the object type. Now just create an instance of the MemoryStream class from System.IO namespace that is going to help us to hold the serialized data. So all your instances are there, now you need to call their methods and get your serialzed object in the xml format. My function to Serialize an object looks like following.

private string SerializeAnObject(object obj)

{

System.Xml.XmlDocument doc = new XmlDocument();

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

System.IO.MemoryStream stream = new System.IO.MemoryStream();

try

{

    serializer.Serialize(stream, obj);

    stream.Position = 0;

    doc.Load(stream);

    return doc.InnerXml;

}

catch

{

    throw;

}

finally

{

    stream.Close();

    stream.Dispose();

}

}

How to DeSerialize an Object

To DeSerialize an object you need an instance of StringReader, XmlReader and XmlSerializer class in order to read the xml data (Serialized data), read it into XmlReader and DeSerialize it respectively. So in brief my function to DeSerialize the object looks like following.

private object DeSerializeAnObject(string xmlOfAnObject)

{

MyClass myObject = new MyClass();

System.IO.StringReader read = new StringReader(xmlOfAnObject);

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());

System.Xml.XmlReader reader = new XmlTextReader(read);

try

{

    myObject = (MyClass)serializer.Deserialize(reader);

    return myObject;

}

catch

{

throw;

}

finally

{

    reader.Close();

    read.Close();

    read.Dispose();

}

}

jrummell
How do you propose he/she saves it to the client pc?
Daniel Dyson
Send the xml to the client as a download I guess, and then the client would need to upload it to make changes. IMO it would make a lot more sense to store it server side.
jrummell
Agreed, but for a small amount of data, cookies are ideal. That is what they are for. As long as you are not absolutely dependent on them being there and they are just used to make it more convenient for the user, so that they don't have to fill in a form again, cookies are a lot quicker to implement than a serverside storage mechanism.
Daniel Dyson
You're right, depending on what the OP is using the data for cookies maybe a better option. If he needs to store it long term, XML on the server side would be ideal. But for small, short term client storage cookies are perfect.
jrummell
This has the most promise, but I will be simplifying things. The data on the form is not important enough for converting to a standalone class, and there are about 20 different forms with varying complexity this methodology needs to work for, all with different fields.Background: This is an upgrade/replacement of old JetForms client/server functionality. We are generating pdfs with dynamicpdf, but we don't have fillable pdfs just yet, due to time constraints.
kaplooeymom
+1  A: 

Some other options for storing data on the client side: See this article which has links to info about

  • HTML5 local storage (requires a new browser)
  • Google gears (has to be installed)
  • Yahoo's SWFStore (requires Flash, which is said to be installed on > 98% of browsers)
  • Cookies (for small amounts of data)

A big question is, are you aiming to support multiple client-side browsers, or just IE? That will be a big factor in determining what method to use.

LarsH
Add to that Silverlight - you are able to store 1MB of data on the client side in Isolated storage. This can be increased, but the user is prompted for permission.
Daniel Dyson