using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;
using DataAnalysisControl.Core;
namespace Aitex.DataAnalysis.Core
{
///
/// class 'ObjectSerializer' provides a set of utilities which serialize objects to xml or binary file,
/// or deserialize xml or binary file back to objects.
///
public sealed class ObjectSerializer
{
private ObjectSerializer() { }
///
/// Serialize the supplied object To memory stream in xml format.
///
///
///
public static MemoryStream SerializeObjectToMemoryStream(object valueToSerializeToMemoryStream)
{
var retVal = new MemoryStream();
var typeToSerialize = valueToSerializeToMemoryStream.GetType();
var serializer = new XmlSerializer(typeToSerialize);
serializer.Serialize(retVal, valueToSerializeToMemoryStream);
retVal.Seek(0, SeekOrigin.Begin);
return retVal;
}
///
///
///
///
///
public static XmlDocument SerializeObjectToXmlDom(object obj)
{
Stream stream = SerializeObjectToMemoryStream(obj);
XmlDocument xmldom = new XmlDocument();
xmldom.Load(stream);
return xmldom;
}
///
/// Serialize the supplied object To binary stream, SeekOrigin set to Begin.
///
///
///
public static MemoryStream SerializeObjectToBinaryStream(object valueToSerializeToMemoryStream)
{
// Converts object [objectToSerialize] of type [typeOfObjectBeingSerialized] to a memory stream
var retVal = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(retVal, valueToSerializeToMemoryStream);
retVal.Seek(0, SeekOrigin.Begin);
return retVal;
}
///
/// Serializes an object into a file using binary formatter.
///
///
///
///
public static bool SerializeObjectToBinaryFile(string fullFileName, object valueToSerialize)
{
try
{
using (FileStream stream = new FileStream(fullFileName, FileMode.OpenOrCreate))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, valueToSerialize);
stream.Seek(0, SeekOrigin.Begin);
}
}
catch (Exception ex)
{
CONTEXT.WriteLog(ex);
return false;
}
return true;
}
///
/// Serialize the supplied object To Xml stream
///
///
///
public static string SerializeObjectToXml(object valueToSerializeToMemoryStream)
{
using (var memStream = SerializeObjectToMemoryStream(valueToSerializeToMemoryStream))
using (var reader = new StreamReader(memStream))
{
return reader.ReadToEnd();
}
}
///
/// Serializes an object into a Xml file.
///
///
///
///
public static bool SerializeObjectToXmlFile(string fullFileName, object valueToSerializeToFile)
{
try
{
using(FileStream fs = new FileStream(fullFileName, FileMode.Create))//file existed, use "FileMode.CreateNew" will cause exception
{
using (MemoryStream ms = SerializeObjectToMemoryStream(valueToSerializeToFile))
{
ms.WriteTo(fs);
ms.Close();
}
fs.Close();
}
}
catch (Exception e)
{
CONTEXT.WriteLog(e);
return false;
}
return true;
}
public static T DeserializeObjectFromStream(Stream stream)
{
var serializer = new XmlSerializer(typeof(T));
var retVal = (T)serializer.Deserialize(stream);
return retVal;
}
///
/// Convert binary file content to object
///
///
///
///
public static T DeserializeObjectFromBinaryFile(string binFilePath)
{
T obj = default(T);
using (FileStream fs = new FileStream(binFilePath, FileMode.OpenOrCreate))
{
obj = (T)DeserializeObjectFromBinaryStream(fs);
}
return obj;
}
///
/// Deserialize supplied stream using BinaryFormatter
///
///
///
public static object DeserializeObjectFromBinaryStream(Stream stream)
{
IFormatter formatter = new BinaryFormatter();
var retVal = formatter.Deserialize(stream);
return retVal;
}
public static object DeserializeObjectFromStream(Stream stream, Type typeOfObject)
{
try
{
var serializer = new XmlSerializer(typeOfObject);
object retVal = serializer.Deserialize(stream);
return retVal;
}
catch(Exception ex)
{
CONTEXT.WriteLog(ex, string.Format("界面发序列化数据出错:\n函数名:{0}\n串行数据内容:{1}",
System.Reflection.MethodInfo.GetCurrentMethod().Name,
stream.ToString()));
throw;
}
}
///
/// Convert supplied string to xml stream, then deserialize the xml stream to specified type of object
///
///
///
///
public static object DeserializeObjectFromXml(string xml, Type typeToDeserialize)
{
if (string.IsNullOrWhiteSpace(xml))
{
return null;
}
using (var memoryStream = new MemoryStream())
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
xmlDoc.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return DeserializeObjectFromStream(memoryStream, typeToDeserialize);
}
}
///
/// Convert xml file content to object
///
///
///
///
public static T DeserializeObjectFromXmlFile(string xmlFilePath)
{
T obj = default(T);
FileInfo fi = new FileInfo(xmlFilePath);
if (!fi.Exists) return obj;
using (StreamReader reader = new StreamReader(xmlFilePath))
{
if (string.IsNullOrWhiteSpace(reader.ReadToEnd())) return obj;
}
using (FileStream fs = new FileStream(xmlFilePath, FileMode.Open))
{
obj = DeserializeObjectFromStream(fs);
}
return obj;
}
}
}