Until now, I only had to store application state of primitive types like int, string, boolean and for that I used Pro开发者_开发技巧perties.Settings.Default
object with no problem.
But now I also need to store instances of classes created with new
keyword.
So my question is how to store these types of objects?
Serialize your object with XmlSerializer in that way:
var p=new Person();
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(Console.Out, p);
Source and full example: http://support.microsoft.com/kb/815813
For the complete process - serializing and deserializing - visit this link:
http://sharpertutorials.com/serialization/
Notes:
- you have to use the attribute [Serializable] (or other, it depends on the serializer) on your class
- try using POCO objects
- depending on your .NET version, consider toughts in this Codeproject article.
You can use msdn: ObjectSerialization.
You have to read a couple of articles in order to select right serialization mechanism for your objects.
Regards.
精彩评论