以下是一个包装的用于序列化反序列化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
}