The number of four articles in one article is mainly used for XML and Binary serialization and deserialization.

Source: Internet
Author: User

Using System. IO; using System. runtime. serialization. formatters. binary; using System. xml. serialization; namespace NOAS. publicOpinionMonitor. common {public class SerializeUtility {public enum SerializeType {XML, binary} # region serialization // <summary> // serialization // </summary> // <typeparam name = "T"> </typeparam> /// <param name = "t"> </param> /// <param name = "savePath"> </param> /// <param name = "type"> </ param> Public static void Serialize <T> (T t, string savePath, SerializeType type) {switch (type) {case SerializeType. XML: SerializeToXML <T> (t, savePath); break; case SerializeType. binary: SerializeToBinary <T> (t, savePath); break; default: break ;}} /// <summary> /// serialize the file to an XML Format File /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "t"> </param> // <param name = "savePath"> </param> private s Tatic void SerializeToXML <T> (T t, string savePath) {if (! Directory. getParent (savePath ). exists) Directory. getParent (savePath ). create (); XmlSerializer ser = new XmlSerializer (t. getType (); if (File. exists (savePath) {File. delete (savePath);} using (FileStream stream = new FileStream (savePath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {ser. serialize (stream, t) ;}/// <summary> // Serialize the file to Binary format /// </summary> /// <typeparam nam E = "T"> </typeparam> // <param name = "t"> </param> // <param name = "savePath"> </param> private static void SerializeToBinary <T> (T t, string savePath) {if (! Directory. getParent (savePath ). exists) Directory. getParent (savePath ). create (); BinaryFormatter serilazer = new BinaryFormatter (); using (FileStream stream = new FileStream (savePath, FileMode. create, FileAccess. readWrite, FileShare. readWrite) {serilazer. serialize (stream, t) ;}# endregion # region deserialization public static T DeSerialize <T> (string readPath, SerializeType type) {switch (type) {case Serial IzeType. XML: return DeSerializeXML <T> (readPath); case SerializeType. binary: return DeSerializeBinary <T> (readPath); default: return default (T );}} /// <summary> /// deserialize the XML file /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "savePath"> </param> // <returns> </returns> private static T DeSerializeXML <T> (string readPath) {if (! File. exists (readPath) {return default (T);} XmlSerializer ser = new XmlSerializer (typeof (T); using (FileStream stream = new FileStream (readPath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {return (T) ser. deserialize (stream );}} /// <summary> /// deserialize the Binary file /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "savePath"> </param> // <returns> </returns> private static T DeSerializeBinary <T> (string readPath) {BinaryFormatter serilazer = new BinaryFormatter (); using (FileStream stream = new FileStream (readPath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {return (T) serilazer. deserialize (stream) ;}# endregion }}


This method is used to directly read files.

Related Article

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.