基本信息
源码名称:C# xml序列化和反序列化
源码大小:3.46KB
文件格式:.zip
开发语言:C#
更新时间:2017-08-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
C# xml序列化和反序列化

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace XStream
{
    public class XmlSerializeUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">xml字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            object obj = null;
            try
            {
                using (StringReader str = new StringReader(xml))
                {
                    XmlSerializer xmlNode = new XmlSerializer(type);
                    obj = xmlNode.Deserialize(str);
                }
            }
            catch(System.InvalidOperationException e)
            {

            }
            return obj;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object Deserialize(Type type,Stream stream)
        {
            Object obj = null;
            try
            {
                XmlSerializer xmlNode = new XmlSerializer(type);
                obj = xmlNode.Deserialize(stream);
            }
            catch(System.InvalidOperationException e)
            {

            }
            return obj;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(Type type, object obj)
        {
            MemoryStream stream = new MemoryStream();
            XmlSerializer xmlNode = new XmlSerializer(type);
            xmlNode.Serialize(stream, obj);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            string str = reader.ReadToEnd();

            stream.Dispose();
            reader.Dispose();
            return str;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace XStream
{
    [XmlRoot("PublicSetting")]
    public class ConfigEntity
    {
        private SystemNode _systemNode;
        private Other _other;

        [XmlElement("Other")]
        public Other other
        {
            set { _other = value; }
            get { return _other; }
        }

        [XmlElement("System")]
        public SystemNode systemNode
        {
            set {_systemNode = value; }
            get { return _systemNode; }
        }

        //         public DBConfig dbConfig
        //         {
        //             set { _dbConfig = value; }
        //             get { return _dbConfig; }
        //         }

        private List<DatabaseConfig> dataBase = new List<DatabaseConfig>();

        [XmlArray("DBConfig")]
        [XmlArrayItem("DatabaseConfig")]
        public List<DatabaseConfig> DBConfig
        {
           get { return dataBase; }
        }


    }
   
    public class Other
    {
        private int _encoding;

        [XmlElement("Encoding")]
        public int encoding
        {
            set { _encoding = value; }
            get { return _encoding; }
        }
    }
    //[XmlRootAttribute("System")]
    public class SystemNode
    {
        private string _sysVip;
        private string _cmServer;
        private string _cmServerWin;
        private string _cmWeb;
        private string _fls;
        private string _flSvr;
        private string _jove;
        private string _plsWin;
        private string _ingestDbSvr;
        private string _ingestDevCtl;
        private string _ingestTaskSvr;
        private string _ingestMsgSvr;
        private string _otc;
        private string _sangHa;
        private string _sangHaSvr;
        private string _sangHaWeb;
        private string _snsSvr;

        [XmlElement("Sys_VIP")]
        public string sysVip
        {
            set { _sysVip = value; }
            get { return _sysVip; }
        }

        [XmlElement("CMServer")]
        public string cmServer
        {
            set { _cmServer = value; }
            get { return _cmServer; }
        }
        [XmlElement("CMserver_windows")]
        public string cmSerWin
        {
            set { _cmServerWin = value; }
            get { return _cmServerWin; }
        }
        [XmlElement("CMweb")]
        public string cmWeb
        {
            set { _cmWeb = value; }
            get { return _cmWeb; }
        }
        [XmlElement("Fls")]
        public string fls
        {
            set { _fls = value; }
            get { return _fls; }
        }
        [XmlElement("FLSvr")]
        public string flsSvr
        {
            set { _flSvr = value; }
            get { return _flSvr; }
        }

        [XmlElement("Jove")]
        public string jove
        {
            set { _jove = value; }
            get { return _jove; }
        }

        [XmlElement("PLS_Windows")]
        public string plsWin
        {
            set { _plsWin = value; }
            get { return _plsWin; }
        }
        [XmlElement("IngestDBSvr")]
        public string ingestDbSvr
        {
            set { _ingestDbSvr = value; }
            get { return _ingestDbSvr; }
        }
        [XmlElement("IngestDEVCTL")]
        public string ingestDevCtl
        {
            set { _ingestDevCtl = value; }
            get { return _ingestDevCtl; }
        }
        [XmlElement("IngestTaskSvr")]
        public string ingestTaskSvr
        {
            set { _ingestTaskSvr = value; }
            get { return _ingestTaskSvr; }
        }
        [XmlElement("IngestMsgSvr")]
        public string ingestMsgSvr
        {
            set { _ingestMsgSvr = value; }
            get { return _ingestMsgSvr; }
        }
        [XmlElement("Otc")]
        public string otc
        {
            set { _otc = value; }
            get { return _otc; }
        }
        [XmlElement("SangHa")]
        public string sangHa
        {
            set { _sangHa = value; }
            get { return _sangHa; }
        }
        [XmlElement("SangHaSvr")]
        public string sangHaSvr
        {
            set { _sangHaSvr = value; }
            get { return _sangHaSvr; }
        }

        [XmlElement("SangHaWeb")]
        public string sangHaWeb
        {
            set { _sangHaWeb = value; }
            get { return _sangHaWeb; }
        }

        [XmlElement("SNSServer")]
        public string snsSvr
        {
            set { _snsSvr = value; }
            get { return _snsSvr; }
        }
    }
    //     [XmlRootAttribute("DBConfig")]
    //     public class DBConfig
    //     {
    // 
    // 
    //     }

    [XmlRootAttribute("DatabaseConfig")]
    public class DatabaseConfig
    {
        private string _moduleName;


        [XmlAttribute("module")]
        public string moduleName
        {
            set { _moduleName = value; }
            get { return _moduleName; }
        }

        private string _instance;
        private string _userName;
        private string _passWord;
        private int _port;

        [XmlElement("Instance")]
        public string instance
        {
            set { _instance = value; }
            get { return _instance; }
        }
        [XmlElement("Username")]
        public string userName
        {
            set { _userName = value; }
            get { return _userName; }
        }
        [XmlElement("Password")]
        public string passWord
        {
            set { _passWord = value; }
            get { return _passWord; }
        }
        [XmlElement("Port")]
        public int port
        {
            set { _port = value; }
            get { return _port; }
        }
    }
}