CharlesChen's Technical Space

简单实用是我一直在软件开发追求的目标(I Focus on. Net technology, to make the greatest efforts to enjoy the best of life.)
Not the best, only better
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

Xmlserializer序列和反序列化方法(XML格式)总结

Posted on 2009-01-03 22:40  Charles Chen  阅读(8576)  评论(2编辑  收藏  举报

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
    }