• C# 序列化反序列化XML的帮助类


    以下是一个包装的用于序列化反序列化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
        }
  • 相关阅读:
    写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我
    WCF快速上手(二)
    Oracle递归查询
    100多行代码实现6秒完成50万条多线程并发日志文件写入
    C#写日志工具类
    WPF定时刷新UI界面
    HttpUtil工具类
    WPF GridView的列宽度设置为按比例分配
    Visifire图表
    C# BackgroundWorker 的使用、封装
  • 原文地址:https://www.cnblogs.com/muzizongheng/p/3169041.html
Copyright © 2020-2023  润新知