以下是一个包装的用于序列化反序列化XML和C# 对象的类。
 public class XmlSerializeHelper<T>
    {
        #region Serialize/Deserialize
        private static System.Xml.Serialization.XmlSerializer serializer;
        private static System.Xml.Serialization.XmlSerializer Serializer
        {
            get
            {
                if ((serializer == null))
                {
                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                }
                return serializer;
            }
        }
        /// <summary>
        /// Serializes object into an XML document
        /// </summary>
        /// <returns>string XML value</returns>
        public virtual string Serialize()
        {
            System.IO.StreamReader streamReader = null;
            System.IO.MemoryStream memoryStream = null;
            try
            {
                memoryStream = new System.IO.MemoryStream();
                Serializer.Serialize(memoryStream, this);
                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                streamReader = new System.IO.StreamReader(memoryStream);
                return streamReader.ReadToEnd();
            }
            catch
            {
               
            }
            finally
            {
                if ((streamReader != null))
                {
                    streamReader.Dispose();
                }
                if ((memoryStream != null))
                {
                    memoryStream.Dispose();
                }
            }
            return "";
        }
        /// <summary>
        /// Deserialize xml string into an  object
        /// </summary>
        /// <param name="xml">string to deserialize</param>
        /// <param name="obj">Output 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 T obj, out System.Exception exception)
        {
            exception = null;
            obj = default(T);
            try
            {
                obj = Deserialize(xml);
                return true;
            }
            catch (System.Exception ex)
            {
                exception = ex;
                return false;
            }
        }
        /// <summary>
        /// Deserialize xml string into an  object
        /// </summary>
        /// <param name="xml">string to deserialize</param>
        /// <param name="obj">Output object</param>
        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool Deserialize(string xml, out T obj)
        {
            System.Exception exception = null;
            return Deserialize(xml, out obj, out exception);
        }
        /// <summary>
        /// Deserialize xml file into  construct.
        /// </summary>
        /// <param name="xml">string to deserialize</param>
        /// <returns>object if this XmlSerializer can deserialize the object</returns>
        public static T Deserialize(string xml)
        {
            System.IO.StringReader stringReader = null;
            try
            {
                stringReader = new System.IO.StringReader(xml);
                return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
            }
            catch
            {
                //
            }
            finally
            {
                if ((stringReader != null))
                {
                    stringReader.Dispose();
                }
            }
            return default(T);
        }
        /// <summary>
        /// Serializes current object into file
        /// </summary>
        /// <param name="fileName">full path of output 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
            {
                SaveToFile(fileName);
                return true;
            }
            catch (System.Exception e)
            {
                exception = e;
                return false;
            }
        }
        /// <summary>
        /// Serializes current object into file
        /// </summary>
        /// <param name="fileName">full path of output xml file</param>
        /// <returns>true if can serialize and save into file; otherwise, false</returns>
        public virtual void SaveToFile(string fileName)
        {
            System.IO.StreamWriter streamWriter = null;
            try
            {
                string xmlString = Serialize();
                System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
                streamWriter = xmlFile.CreateText();
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
            }
            catch
            {
                //
            }
            finally
            {
                if ((streamWriter != null))
                {
                    streamWriter.Dispose();
                }
            }
        }
        /// <summary>
        /// Deserialize xml markup from file into an  object
        /// </summary>
        /// <param name="fileName">string xml file to load and deserialize</param>
        /// <param name="obj">Output  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 T obj, out System.Exception exception)
        {
            exception = null;
            obj = default(T);
            try
            {
                obj = LoadFromFile(fileName);
                return true;
            }
            catch (System.Exception ex)
            {
                exception = ex;
                return false;
            }
        }
        /// <summary>
        /// Deserialize xml markup from file into an  object
        /// </summary>
        /// <param name="fileName">string xml file to load and deserialize</param>
        /// <param name="obj">Output  object</param>
        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool LoadFromFile(string fileName, out T obj)
        {
            System.Exception exception = null;
            return LoadFromFile(fileName, out obj, out exception);
        }
        /// <summary>
        /// Deserialize xml markup from file into an  object
        /// </summary>
        /// <param name="fileName">string xml file to load and deserialize</param>
        /// <returns></returns>
        public static T LoadFromFile(string fileName)
        {
            System.IO.FileStream file = null;
            System.IO.StreamReader sr = null;
            try
            {
                file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
                sr = new System.IO.StreamReader(file);
                string xmlString = sr.ReadToEnd();
                sr.Close();
                file.Close();
                return Deserialize(xmlString);
            }
            catch
            {
            }
            finally
            {
                if ((file != null))
                {
                    file.Dispose();
                }
                if ((sr != null))
                {
                    sr.Dispose();
                }
            }
            return default(T);
        }
        #endregion
    }

C# 序列化反序列化XML的帮助类的更多相关文章

  1. 学习C# XmlSerializer 序列化反序列化XML

    类.变量常用头: [XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类 [XmlType]:对节点描述,在类声明中使用         如:下例的Head类 [X ...

  2. 05-06 Flutter JSON和序列化反序列化、创建模型类转换Json数据、轮播图数据渲染:Flutter创建商品数据模型 、请求Api接口渲染热门商品 推荐商品

    Config.dart class Config{ static String domain='http://jd.itying.com/'; } FocusModel.dart class Focu ...

  3. c# XML和实体类之间相互转换(序列化和反序列化)[砖]

    link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...

  4. C# XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...

  5. XML序列化反序列化—常用类

    public class XMLSerializer    {        #region (public) xml序列化        /// <summary>        /// ...

  6. XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  7. 用C#实现XML和实体类之间序列化和反序列化相互转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. 对类参数的序列化和反序列化XML

    /// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...

  9. php json与xml序列化/反序列化

    在web开发中对象的序列化与反序列化经常使用,比较主流的有json格式与xml格式的序列化与反序列化,今天想写个jsop的小demo,结果发现不会使用php序列化,查了一下资料,做个笔记 简单数组js ...

随机推荐

  1. c# 根据枚举Value 获得名称

    // 定义枚举类型enum sotype : int { book=1, pen=2, other=3 } // 输出名称 switch (Enum.GetName(typeof(sotype), 1 ...

  2. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...

  3. EasyPlayerPro windows播放器在播放RTMP视频显示重复异常问题解决

    问题来源 2017.12.18 今日有杭州某教育领域客户反馈EasyPlayerPro在播放一个rtmp源时,画面显示异常的问题.截图如下: 问题复现 一番思考, 将显示格式改为D3D显示, 正常, ...

  4. Bloom Filters

    http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html A Bloom filter is a method for represe ...

  5. 【题解】[CJOI2019]Cipher

    [题解][CJOI2019]Cipher 题目描述 给定你\(p\)进制数\(s\),\(p \le 9+26\),求对于十进制数\(k\),求\(k^s \equiv ? \mod m\) 数据范围 ...

  6. Java异步编程第2篇

    假如如今有一个Buttonbutton,Buttonbutton上有click和doubleclick事件. 两个不同的事件须要进行不同的处理.这时候就须要为对应的事件注冊Listener了.改动后的 ...

  7. .net 调用SAP RFC的几种方法

    转自:http://www.cherpservice.com/pub/newsdetail.asp?Newsid=3613 第一种方式采用SAP.net Connector: 最新版本是3.,不开源, ...

  8. php 获取优酷视频的真实地址(2014.6月新算法)

    上个礼拜发现优酷改版了,各种过滤优酷广告的插件都失效了,于是我百度了一下(谷歌也不能用了)发现优酷改算法了,在ckplayer论坛发现有人在6月25号发了个php 的优酷代理文件,下载下来发现,能用但 ...

  9. [2018-11-27]2018年12月1日宁波dotnet社区线下活动

    离上次活动,转眼又过了一个月,幸得各路大神支持,于本周六(12月1日),宁波dotnet社区的线下分享活动又来啦! 活动嘉宾及主题 董斌辉 2015-2019年微软全球最有价值专家(.NET方向) 2 ...

  10. [2017-12-20]ElasticSearch 小记

    介绍 ElasticSearch是一款搜索引擎中间件,因其强大的全文索引.查询统计能力和非常方便的全套基于Restful的接口,以及在自动分片.无停机升级扩容.故障转移等运维方面的高效性,逐渐成为中小 ...