. Net serialization and deserialization Serializerhelper

Source: Internet
Author: User

I started thinking that the Serializerhelper class was already included in the project, and later testing the code in another solution found that the Serializerhelper class was written by itself.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Text;usingSystem.IO;usingSystem.Xml.Serialization;usingNewtonsoft.json;/// <summary>///Summary description of Serializerhelper/// </summary> Public Static classserializerhelper{/// <summary>    ///deserializing XML Files/// </summary>     Public StaticT loadfromxmlfile<t> (stringFilePathwhereT:class    {        using(FileStream stream =NewFileStream (filepath, FileMode.Open, FileAccess.Read)) {XmlSerializer Serializer=NewXmlSerializer (typeof(T)); return(T) serializer.        Deserialize (stream); }    }    /// <summary>    ///deserialize an XML string/// </summary>     Public StaticT loadfromxmlstring<t> (stringxmlwhereT:class{XmlSerializer Serializer=NewXmlSerializer (typeof(T)); byte[] bytes =Encoding.UTF8.GetBytes (XML); using(MemoryStream stream =NewMemoryStream (bytes)) {            return(T) serializer.        Deserialize (stream); }    }    /// <summary>    ///Serializing XML Objects/// </summary>     Public Static stringSavetoxmlstring<t> (T entity)whereT:class    {        using(MemoryStream stream =NewMemoryStream ()) {XmlSerializer Serializer=NewXmlSerializer (typeof(T)); Serializer.            Serialize (Stream, entity); returnEncoding.UTF8.GetString (stream.        ToArray ()); }    }    /// <summary>    ///Serializing JSON Objects/// </summary>     Public Static stringtoJSONString (Objectobj) {        returntojsonstring<Object>(obj); }    /// <summary>    ///Serializing JSON Objects/// </summary>     Public Static stringtojsonstring<t> (T obj)whereT:class    {        stringText =jsonconvert.serializeobject (obj); returntext; }    /// <summary>    ///deserializing JSON strings/// </summary>     Public StaticT tojsonobject<t> (stringTextwhereT:class{T obj= (T) jsonconvert.deserializeobject (text,typeof(T)); returnobj; }}

Newtonsoft.Json.dll (looking for a long while do not know where to add attachments so can only put my upload path):

HTTPS://Files.cnblogs.com/files/swjian/newtonsoft.json.rar

. Net serialization and deserialization Serializerhelper

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.