zoukankan      html  css  js  c++  java
  • 序列化

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    namespace WordDocumentSocket
    {
        /// <summary>
        /// 序列化
        /// </summary>
        public class Serializer
        {
            /// <summary>
            /// 序列化
            /// </summary>
            /// <param name="objectToConvert"></param>
            /// <param name="path"></param>
            /// <param name="encoding"></param>
            public void PositiveSerializer(object objectToConvert, string path, Encoding encoding)
            {
                // 对象不为空
                if (objectToConvert != null)
                {
                    Type t = objectToConvert.GetType();
                    XmlSerializer ser = new XmlSerializer(t);
                    using (StreamWriter writer = new StreamWriter(path, false, encoding))
                    {
                        ser.Serialize(writer, objectToConvert);
                        writer.Close();
                    }
                }
            }

            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="path">路经加文件名</param>
            /// <param name="objectType">内容类型</param>
            /// <param name="encoding">编码类型</param>
            /// <returns></returns>
            public object InsteadSerializer(string path, Type objectType, Encoding encoding)
            {
                object convertedObject = null;
                // 文件名不为空
                if (!string.IsNullOrEmpty(path))
                {
                    XmlSerializer ser = new XmlSerializer(objectType);
                    using (StreamReader reader = new StreamReader(path, encoding))
                    {
                        convertedObject = ser.Deserialize(reader);
                        reader.Close();
                    }
                }
                return convertedObject;
            }
        }
    }

    [Serializable]
        public class UserInfo
        {
            private string ifOpen;

            public string IfOpen
            {
                get { return ifOpen; }
                set { ifOpen = value; }
            }
            private string addressIp;

            public string AddressIp
            {
                get { return addressIp; }
                set { addressIp = value; }
            }
            private int port;

            public int Port
            {
                get { return port; }
                set { port = value; }
            }
        }

    UserInfo userinfo = new UserInfo();
                string path = @"xsm.xml";
                userinfo.IfOpen = "1";
                userinfo.AddressIp = GetIPAddress();
                userinfo.Port = 1027;
                sl.PositiveSerializer(userinfo, path, Encoding.UTF8);
                object o = sl.InsteadSerializer(path, typeof(UserInfo), Encoding.UTF8);
                UserInfo d = o as UserInfo;
                this.Paragraphs[1].Range.Text = "xsmhero "+d.IfOpen+" "+d.AddressIp +"  "+d.Port;

  • 相关阅读:
    Linux Shell常用技巧(二)
    Linux Shell常用技巧(一)
    Linux Shell常用命令总结
    不大于N的所有素数
    include指令
    PotPlayer 进度条显示缩略图
    PotPlayer 禁止更新
    a标签添加点击事件
    配置JDK环境变量
    电路交换与分组交换的差别
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1436891.html
Copyright © 2011-2022 走看看