B/S开发框架XML序列化讲解

  快速开发平台B/S开发框架XML序列化,序列化是将对象转换成IO流(与平台无关的二进制流)serialize,而反序列化 从IO流中恢复该对象 deserialize。

序列化的作用:

1)把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
2) 在网络上传送对象的字节序列。

B/S开发框架序列化

B/S开发框架中如何序列化:

 public class XmlSerializerHelper
    {
        public static string GetXmlString(object model)
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            var xmlWriterSettings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = true
            };

            var xs = new XmlSerializer(model.GetType());

            using (var sw = new StringWriter())
            using (var writer = XmlWriter.Create(sw, xmlWriterSettings))
            {
                xs.Serialize(writer, model, ns);
                return sw.ToString();
            }
        }

        public static T FromXmlString<T>(string xmlString) where T : class
        {
            try
            {
                var xmlRreaderSettings = new XmlReaderSettings();

                var xs = new XmlSerializer(typeof(T));

                using (var sr = new StringReader(xmlString))
                using (var reader = XmlReader.Create(sr, xmlRreaderSettings))
                {
                    var result = xs.Deserialize(reader) as T;
                    return result;
                }
            }
           catch(Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("FromXmlString Failed:{0}"), ex.Message);
                return null;
            }
        }
    }

B/S开发框架XML序列化讲解,序列化是什么,为什么要序列化,XML序列化是怎样的?你明白了么? 

本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:软件开发框架 » B/S开发框架XML序列化讲解
本文标题:B/S开发框架XML序列化讲解

原文地址:https://www.cnblogs.com/frfwef/p/14573534.html