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