C # xmlserializer

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.