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.
我有一个web表单,它需要将用户输入的数据保存到一个文件中,并保存在客户机pc上,该文件还可以从保存的文件中读取数据,并在稍后重新填充字段。不会将任何文件保存到服务器端,因此我预计在编写时需要涉及流媒体。
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.
我认为XML是实现这一目标的一种简单方法,但我对方法感到困惑。XML文档吗?XML的作家?我被正确的搜索条件难住了,甚至得到了我想要的。
Thanks in advance for any pointers.
谢谢你的指点。
3 个解决方案
#1
1
You'll want to use XML serialization. Take a look at this MSDN article. Here's an excerpt on serialization and deserialization:
您将希望使用XML序列化。看看这篇MSDN文章。以下是关于序列化和反序列化的摘录:
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.
为了序列化和对象,我们需要很少的内建类的实例。因此,让我们首先从System中创建XmlDocument类的实例。Xml名称空间。然后从System.Xml创建XmlSerializer类的实例。以参数作为对象类型的序列化命名空间。现在只需从System中创建MemoryStream类的实例。IO命名空间,它将帮助我们保存序列化的数据。所有实例都在那里,现在需要调用它们的方法,以xml格式获取序列化的对象。我的函数序列化一个对象看起来如下。
private string SerializeAnObject(object obj)
私人字符串SerializeAnObject(对象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.
为了反序列化一个对象,您需要一个StringReader、XmlReader和XmlSerializer类的实例来读取xml数据(序列化数据),并将其读入XmlReader并将其反序列化。简单地说,我反序列化对象的函数如下所示。
private object DeSerializeAnObject(string xmlOfAnObject)
私有对象DeSerializeAnObject(字符串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(); }
}
}
#2
1
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)
- HTML5本地存储(需要一个新的浏览器)
- Google gears (has to be installed)
- 谷歌档(必须安装)
- Yahoo's SWFStore (requires Flash, which is said to be installed on > 98% of browsers)
- 雅虎的SWFStore(要求Flash,据说安装在> 98%的浏览器上)
- Cookies (for small amounts of data)
- Cookies(用于少量数据)
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.
一个大问题是,您的目标是支持多个客户端浏览器,还是仅仅支持IE?这将是决定使用哪种方法的一个重要因素。
#3
0
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.
听起来您需要将数据存储在cookie中。您不能从浏览器写到客户端pc上的文件。浏览器在沙箱中运行,沙箱保护客户端pc免受恶意网站的攻击。
Take a look at this page on saving and retrieving cookie data to/from a client machine:
查看关于保存和检索客户端机器上的cookie数据的页面:
http://www.aspnettutorials.com/tutorials/network/cookies-csharp.aspx
http://www.aspnettutorials.com/tutorials/network/cookies-csharp.aspx
#1
1
You'll want to use XML serialization. Take a look at this MSDN article. Here's an excerpt on serialization and deserialization:
您将希望使用XML序列化。看看这篇MSDN文章。以下是关于序列化和反序列化的摘录:
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.
为了序列化和对象,我们需要很少的内建类的实例。因此,让我们首先从System中创建XmlDocument类的实例。Xml名称空间。然后从System.Xml创建XmlSerializer类的实例。以参数作为对象类型的序列化命名空间。现在只需从System中创建MemoryStream类的实例。IO命名空间,它将帮助我们保存序列化的数据。所有实例都在那里,现在需要调用它们的方法,以xml格式获取序列化的对象。我的函数序列化一个对象看起来如下。
private string SerializeAnObject(object obj)
私人字符串SerializeAnObject(对象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.
为了反序列化一个对象,您需要一个StringReader、XmlReader和XmlSerializer类的实例来读取xml数据(序列化数据),并将其读入XmlReader并将其反序列化。简单地说,我反序列化对象的函数如下所示。
private object DeSerializeAnObject(string xmlOfAnObject)
私有对象DeSerializeAnObject(字符串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(); }
}
}
#2
1
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)
- HTML5本地存储(需要一个新的浏览器)
- Google gears (has to be installed)
- 谷歌档(必须安装)
- Yahoo's SWFStore (requires Flash, which is said to be installed on > 98% of browsers)
- 雅虎的SWFStore(要求Flash,据说安装在> 98%的浏览器上)
- Cookies (for small amounts of data)
- Cookies(用于少量数据)
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.
一个大问题是,您的目标是支持多个客户端浏览器,还是仅仅支持IE?这将是决定使用哪种方法的一个重要因素。
#3
0
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.
听起来您需要将数据存储在cookie中。您不能从浏览器写到客户端pc上的文件。浏览器在沙箱中运行,沙箱保护客户端pc免受恶意网站的攻击。
Take a look at this page on saving and retrieving cookie data to/from a client machine:
查看关于保存和检索客户端机器上的cookie数据的页面:
http://www.aspnettutorials.com/tutorials/network/cookies-csharp.aspx
http://www.aspnettutorials.com/tutorials/network/cookies-csharp.aspx