zoukankan      html  css  js  c++  java
  • Xmlserializer序列和反序列化方法(XML格式)总结

    XML作为数据交换格式已经是很常见了。那么我们经常要做的工作就是XML和对象之间互相转换,下面是一些方法的总结:供以后参考:

     public class ObjectXmlSerializer
        {
            
    /// <summary>
            
    /// deserialize an object from a file.
            
    /// 
            
    /// 
            
    /// </summary>
            
    /// <typeparam name="T"></typeparam>
            
    /// <param name="m_FileNamePattern"></param>
            
    /// <returns>
            
    /// loggingEnabled==true: Null is returned if any error occurs.
            
    /// loggingEnabled==false: throw exception
            
    /// </returns>
            public static T LoadFromXml<T>(string fileName) where T : class
            {
                
    return LoadFromXml<T>(fileName, true);
            }
            
            
    public static T LoadFromXml<T>(string fileName, bool loggingEnabled) where T : class
            {
                FileStream fs 
    = null;
                
    try
                {
                    XmlSerializer serializer 
    = new XmlSerializer(typeof(T));
                    fs 
    = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    
    return (T)serializer.Deserialize(fs);
                }
                
    catch (Exception e)
                {
                    
    if (loggingEnabled)
                    {
                        LogLoadFileException(fileName, e);
                        
    return null;
                    }
                    
    else throw;
                }
                
    finally
                {
                    
    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }

            
    /// <summary>
            
    /// serialize an object to a file.
            
    /// 
            
    /// </summary>
            
    /// <typeparam name="T"></typeparam>
            
    /// <param name="m_FileNamePattern"></param>
            
    /// <returns>
            
    /// loggingEnabled==true: log exception
            
    /// loggingEnabled==false: throw exception
            
    /// </returns>
            public static void SaveToXml<T>(string fileName, T data) where T : class
            {
                SaveToXml
    <T>(fileName, data, true);
            }        
     
            
    public static void SaveToXml<T>(string fileName, T data, bool loggingEnabled) where T : class
            {
                FileStream fs 
    = null;
                
    try
                {

                    XmlSerializer serializer 
    = new XmlSerializer(typeof(T));
                    fs 
    = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                    serializer.Serialize(fs, data);
                }
                
    catch (Exception e)
                {
                    
    if (loggingEnabled) LogSaveFileException(fileName, e);
                    
    else throw;
                }
                
    finally
                {
                    
    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }

            
    /// <summary>
            
    /// XML & Datacontract Serialize & Deserialize Helper
            
    /// </summary>
            
    /// <typeparam name="T"></typeparam>
            
    /// <param name="serialObject"></param>
            
    /// <returns></returns>
            public static string XmlSerializer<T>(T serialObject) where T : class
            {
                XmlSerializer ser 
    = new XmlSerializer(typeof(T));
                System.IO.MemoryStream mem 
    = new MemoryStream();
                XmlTextWriter writer 
    = new XmlTextWriter(mem, Encoding.UTF8);
                ser.Serialize(writer, serialObject);
                writer.Close();

                
    return Encoding.UTF8.GetString(mem.ToArray());
            }

            
    public static T XmlDeserialize<T>(string str) where T : class
            {
                XmlSerializer mySerializer 
    = new XmlSerializer(typeof(T));
                StreamReader mem2 
    = new StreamReader(
                        
    new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)),
                        System.Text.Encoding.UTF8);

                
    return (T)mySerializer.Deserialize(mem2);
            }


            
    public static T DataContractDeserializer<T>(string xmlData) where T : class
            {
                MemoryStream stream 
    = new MemoryStream(Encoding.UTF8.GetBytes(xmlData));

                XmlDictionaryReader reader 
    =
                    XmlDictionaryReader.CreateTextReader(stream, 
    new XmlDictionaryReaderQuotas());
                DataContractSerializer ser 
    = new DataContractSerializer(typeof(T));
                T deserializedPerson 
    = (T)ser.ReadObject(reader, true);
                reader.Close();
                stream.Close();

                
    return deserializedPerson;
            }

            
    public static string DataContractSerializer<T>(T myObject) where T : class
            {
                MemoryStream stream 
    = new MemoryStream();
                DataContractSerializer ser 
    = new DataContractSerializer(typeof(T));
                ser.WriteObject(stream, myObject);
                stream.Close();

                
    return System.Text.UnicodeEncoding.UTF8.GetString(stream.ToArray());
            }
     
            
    #region Logging
            
    private const string LogCategory = "Framework.ObjectXmlSerializer";
            
    private const int LogEventLoadFileException = 1;

            [Conditional(
    "TRACE")]
            
    private static void LogLoadFileException(string fileName, Exception ex)
            {
                StringBuilder sb 
    = new StringBuilder();
                sb.Append(
    "Fail to load xml file: ");
                sb.Append(fileName 
    + System.Environment.NewLine);
                sb.Append(ex.ToString());
                Logger.LogEvent(LogCategory, LogEventLoadFileException, sb.ToString());
            }

            [Conditional(
    "TRACE")]
            
    private static void LogSaveFileException(string fileName, Exception ex)
            {
                StringBuilder sb 
    = new StringBuilder();
                sb.Append(
    "Fail to save xml file: ");
                sb.Append(fileName 
    + System.Environment.NewLine);
                sb.Append(ex.ToString());
                Logger.LogEvent(LogCategory, LogEventLoadFileException, sb.ToString());
            }
            
    #endregion
        }
  • 相关阅读:
    C#输出JS代码封装类Alart
    我的汇编学习之路(2)win8(64位)下安装debug
    .NET使用一般处理程序生成验证码
    ?运算符(null合并运算符)和三木运算符
    讲解:小菜鸟自制的.NET实体类生成器
    我的汇编学习之路(1)进制转换
    未来
    callee,caller区别
    string::size_type
    ubuntu 12.04 LTS u盘安装
  • 原文地址:https://www.cnblogs.com/Charles2008/p/1367679.html
Copyright © 2011-2022 走看看