Convert Between class and XML

Source: Internet
Author: User

Public t deserializer <t> (string serializer)

{

Datacontractserializer SER = new datacontractserializer (typeof (t ));

Memorystream MS = new memorystream (encoding. utf8.getbytes (serializer ));

T datatractdto = (t) SER. readobject (MS );

Ms. Close ();

Return datatractdto;

}

Public String serialize <t> (T instance)

{

Datacontractserializer SER = new datacontractserializer (instance. GetType ());

Using (memorystream MS = new memorystream ())

{
Ser. writeobject (MS, instance );
Stringbuilder sb = new stringbuilder ();
SB. append (encoding. utf8.getstring (Ms. toarray ()));
Return sb. tostring ();
}

}

Description:

Datacontractserializer class from namespace of system. runtime. serialization

Memorystream class from namespace of system. Io

Import:

How to deserializer? The XML string contains the namespace of T. it cocould Based on the namespace to find t, then do the deserializer. so, if we change the namespace of T, it will not success because of no finding t by namespace!

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.