Article From:https://www.cnblogs.com/nanguoyezi/p/9687947.html
1.IniWhat is it? (my understanding of it is the file used to store user configuration information, which is placed on the user’s computer…)
INIfileIt is a fixed standard format.Configuration file. It is composed of simple text and simple structure, often used in Windows operating system, or other operating systems, many programs will also use INI file as a setup program. The Windows operating system later replaced the INI file in the form of a registry. INI file is named from the initials of Initial, which correspond to its purpose, the initializer. Sometimes, INI files are replaced by different extensions, such as “. CFG”,”. CONF”, or “. TXT”.
ps:
The notation uses semicolons to represent (()). The text after the semicolon is all annotated until the end of the line.
      /// <summary>
    /// INIFile read class/// </summary>
    public class INIHelper
    {
        /// <summary>
        /// file name/// </summary>
        public static string str = "setting.ini";
        /// <summary>
        /// File path/// </summary>
        public static string path = System.AppDomain.CurrentDomain.BaseDirectory + str;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);

        /// <summary>
        /// Write a INI file/// </summary>
        /// <param name="Section">Node name</param>
        /// <param name="Key">key</param>
        /// <param name="Value">value</param>
        public static void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }

        /// <summary>
        /// Read INI file/// </summary>
        /// <param name="Section">Node name</param>
        /// <param name="Key">key</param>
        /// <param name="path">File path</param>
        /// <returns></returns>
        public static string IniReadValue(string Section, string Key, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }

        /// <summary>
        /// Read INI file/// </summary>
        /// <param name="section">Node name</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];
            int i = GetPrivateProfileString(section, key, "", temp, 255, path);
            return temp;

        }
        /// <summary>
        /// Delete all nodes under the INI file/// </summary>
        public static void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }
        /// <summary>
        /// Delete all keys from the INI file pointing node./// </summary>
        /// <param name="Section"></param>
        public static void ClearSection(string Section)
        {
            IniWriteValue(Section, null, null);
        }
    }
Source: https://zh.wikipedia.org/wiki/INI%E6%96%87%E4%BB%B6
Reference: https://en.wikipedia.org/wiki/INI_file
2.JsonWhat is it?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
Source: http://www.json.org/
At present, most of the contact comparisons are Newtonsoft. Json, which is encapsulated in a complete package. What is more memorable is that it uses the properties of the field to determine whether the field is serialized and null is not serialized to support the changing needs.
Reference: Newtonsoft https://www.newtonsoft.com/json
  /// <summary>
        /// Deserialize/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// <summary>
        /// serialize/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string DeserializeObject<T>(object obj)
        {
            JsonConvert.SerializeObject(obj);
        }
3.XmlWhat is it? (closer to human language)
In computing, Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The W3C’s XML 1.0 Specification[2] and several other related specifications[3]—all of them free open standards—define XML.[4]
The design goals of XML emphasize simplicity, generality, and usability across the Internet.[5] It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures[6] such as those used in web services.
Several schema systems exist to aid in the definition of XML-based languages, while programmers have developed many application programming interfaces (APIs) to aid the processing of XML data.
Source:https://en.wikipedia.org/wiki/XML
 public class AppConfigXmlHelper
    {
        public static string AppConfig()
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            string strDirectoryPath = Environment.CurrentDirectory + "\\IniJsonXmlTest.EXE.config";
            return strDirectoryPath;
        }
        public static string GetPath(string key)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(AppConfig());
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//appSettings");    //Add, you need to add in your app.config file, < appSetting> < /appSetting>
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
                if (xElem != null)
                    return xElem.GetAttribute("value");
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static void SetPath(string key, string path)
        {
            XmlElement xElem;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppConfig());
            XmlNode node = doc.SelectSingleNode(@"//appSettings");
            xElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
            xElem.SetAttribute("value", path);
            doc.Save(AppConfig());
        }
    }
public class XmlSerialization
    {

        public static System.IO.Stream Serialize(object obj, Encoding encode, System.IO.Stream s)
        {
            TextWriter writer = null;
            writer = new StreamWriter(s, encode);

            XmlSerializer ser = new XmlSerializer(obj.GetType());

            ser.Serialize(writer, obj);
            return s;

        }

        public static MemoryStream Serialize(object obj, Encoding encode)
        {
            MemoryStream s = new MemoryStream();
            Serialize(obj, encode, s);
            s.Position = 0;
            return s;
        }

        public static MemoryStream Serialize(object obj, String encode)
        {
            return Serialize(obj, Encoding.GetEncoding(encode));
        }

        public static MemoryStream Serialize(object obj)
        {
            return Serialize(obj, Encoding.UTF8);
        }

        public static object Deserialize(System.IO.Stream In, Type objType)
        {
            In.Position = 0;
            XmlSerializer ser = new XmlSerializer(objType);
            return ser.Deserialize(In);
        }
    }

    public class XmlSerialization<T>
    {
        public static System.IO.Stream Serialize(T obj, Encoding encode, System.IO.Stream s)
        {
            TextWriter writer = null;
            writer = new StreamWriter(s, encode);

            XmlSerializer ser = new XmlSerializer(typeof(T));

            ser.Serialize(writer, obj);
            return s;
        }

        public static MemoryStream Serialize(T obj, Encoding encode)
        {
            MemoryStream s = new MemoryStream();
            Serialize(obj, encode, s);
            s.Position = 0;
            return s;
        }

        public static MemoryStream Serialize(T obj, String encode)
        {
            return Serialize(obj, Encoding.GetEncoding(encode));
        }

        public static MemoryStream Serialize(T obj)
        {
            return Serialize(obj, Encoding.UTF8);
        }

        public static T Deserialize(System.IO.Stream In)
        {
            In.Position = 0;
            XmlSerializer ser = new XmlSerializer(typeof(T));
            return (T)ser.Deserialize(In);
        }
    }
4.Serializable and Deserialize
  • Serialization: the process of converting data structures or objects into binary strings.
  • Deserialization: The process of converting binary strings generated during serialization into data structures or objects.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
    public static class Serializer
    {

        #region Binary/// <summary>
        /// Serialize binary files/// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public static void SaveBinary(string path, object data)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = File.Create(path);
            bf.Serialize(fs, data);
            fs.Close();
        }

        /// <summary>
        /// Anti serialization of binary files/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T LoadBinary<T>(string path)
        {
            if (!File.Exists(path))
            {
                return default(T);
            }
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = File.OpenRead(path);
            object data = bf.Deserialize(fs);
            fs.Close();
            return (T)data;
        }
        #endregion

        #region xml
        /// <summary>
        /// Serialize XML files/// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <param name="type"></param>
        public static void SaveXml(string path, object data, Type type)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            XmlSerializer xs = new XmlSerializer(type);
            FileStream fs = File.Create(path);
            xs.Serialize(fs, data);
            fs.Close();
        }

        /// <summary>
        /// De serialization of XML files/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T LoadXml<T>(string path, Type type)
        {
            if (!File.Exists(path))
            {
                return default(T);
            }
            XmlSerializer xs = new XmlSerializer(type);
            FileStream fs = File.OpenRead(path);
            object data = xs.Deserialize(fs);
            fs.Close();
            return (T)data;
        }
        #endregion

        #region json
        /// <summary>
        /// Serialization of JSON/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Serialize(object data)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            json.MaxJsonLength = Int32.MaxValue;
            return json.Serialize(data);
        }

        /// <summary>
        /// Anti serialization JSON/// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string json)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            js.MaxJsonLength = Int32.MaxValue;
            return js.Deserialize<T>(json);
        }
        #endregion

    }
github:https://github.com/HuQinL/IniJsonXmlTest
Reference resources:
INIConfiguration file format https://blog.csdn.net/gateway6143/article/details/29554349
Serialization and de serialization of http://www.infoq.com/cn/articles/serialization-and-deserialization

Leave a Reply

Your email address will not be published. Required fields are marked *