Xmlserializer
Serialize an object to an XML document and deserialize an object from an XML document. XML serialization converts the common attributes and fields of an object into a sequence format (XML in this example) process for storage or transmission
Prevent Memory leakage (from msdn)
To improve performance, the XML serialization infrastructure dynamically generates an assembly for serialization and deserialization of specified types. The infrastructure will find and reuse these assemblies. This behavior occurs only when the following constructor is used:
Xmlserializer (type)
Xmlserializer (type, string)
Example
namespace CSharpDemo{ public class Test { public string Id { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { Test t = new Test() { Id = "1", Name = "hello" }; string s = SerializeObj<Test>(t); Console.WriteLine(s); Test tt = (Test)DeserializeObj<Test>(s); Console.ReadLine(); } static T DeserializeObj<T>(string xml) { XmlSerializer xs = new XmlSerializer(typeof(T)); StringReader sr = new StringReader(xml); object obj = xs.Deserialize(sr); sr.Dispose(); return (T)obj; } static string SerializeObj<T>(T t) { XmlSerializer xs = new XmlSerializer(typeof(T)); StringWriter sw = new StringWriter(); xs.Serialize(sw, t); return sw.ToString(); } }}
For more details, refer to msdn