C#类的序列化与反序列化

 /// <summary>
        /// Serializes current Message object into an XML document
        /// </summary>
        // <returns>string XML value</returns>
        public virtual string Serialize() {
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            xmlSerializer.Serialize(memoryStream, this);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            System.IO.StreamReader streamReader = new System.IO.StreamReader(memoryStream);
            return streamReader.ReadToEnd();
        }
       
        /// <summary>
        /// Deserializes workflow markup into an Message object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output Message object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool Deserialize(string xml, out Message obj, out System.Exception exception) {
            exception = null;
            obj = null;
            try {
                System.IO.StringReader stringReader = new System.IO.StringReader(xml);
                System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader(stringReader);
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Message));
                obj = ((Message)(xmlSerializer.Deserialize(xmlTextReader)));
                return true;
            }
            catch (System.Exception ex) {
                exception = ex;
                return false;
            }
        }
       
        /// <summary>
        /// Serializes current Message object into file
        /// </summary>
        // <param name="fileName">full path of outupt xml file</param>
        // <param name="exception">output Exception value if failed</param>
        // <returns>true if can serialize and save into file; otherwise, false</returns>
        public virtual bool SaveToFile(string fileName, out System.Exception exception) {
            exception = null;
            try {
                string xmlString = Serialize();
                System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
                System.IO.StreamWriter streamWriter = xmlFile.CreateText();
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
                return true;
            }
            catch (System.Exception e) {
                exception = e;
                return false;
            }
        }
       
        /// <summary>
        /// Deserializes workflow markup from file into an Message object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output Message object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool LoadFromFile(string fileName, out Message obj, out System.Exception exception) {
            exception = null;
            obj = null;
            try {
                System.IO.FileStream file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
                System.IO.StreamReader sr = new System.IO.StreamReader(file);
                string xmlString = sr.ReadToEnd();
                sr.Close();
                file.Close();
                return Deserialize(xmlString, out obj, out exception);
            }
            catch (System.Exception ex) {
                exception = ex;
                return false;
            }
        }
       
        /// <summary>
        /// Create a clone of this Message object
        /// </summary>
        public virtual Message Clone() {
            return ((Message)(this.MemberwiseClone()));
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值