using System;
using System.Collections;
using System.IO;
namespace Quiz.Utility
{
///
/// XML对象序列化。
///
public class XmlSerializer
{
static Hashtable serializers;
static XmlSerializer() { serializers = new Hashtable(); }
static System.Xml.Serialization.XmlSerializer GetXmlSerializer(Type type)
{
System.Xml.Serialization.XmlSerializer ser = (System.Xml.Serialization.XmlSerializer)serializers[type];
if (ser == null)
{
ser = new System.Xml.Serialization.XmlSerializer(type, new System.Xml.Serialization.XmlRootAttribute() { ElementName = "Body", Namespace = "" });
serializers[type] = ser;
}
return ser;
}
///
/// 将对象序列化成xml字符串。
///
///
///
public static string Serialize(object o)
{
if (o == null) return null;
System.IO.MemoryStream ms = null;
try
{
ms = new System.IO.MemoryStream();
var xmlserializer = GetXmlSerializer(o.GetType());
xmlserializer.Serialize(ms, o, new System.Xml.Serialization.XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", "") }));
string xml = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
ms.Close(); ms.Dispose(); ms = null;
return xml;
}
catch { return null; }
finally { if (ms != null) { ms.Close(); ms.Dispose(); } }
}
///
/// 将xml文档反序列化成对象。
///
///
///
///
public static object Deserialize(Type type, string xml)
{
if (type == null || string.IsNullOrEmpty(xml)) return null;
System.IO.MemoryStream ms = null;
try
{
var xmlserializer = GetXmlSerializer(type);
ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml));
object o = xmlserializer.Deserialize(ms);
ms.Close(); ms.Dispose(); ms = null;
return o;
}
catch { return null; }
finally { if (ms != null) { ms.Close(); ms.Dispose(); } }
}
///
/// 将对象序列化成xml字符串。
///
///
///
public static string SerializeGoole(object o)
{
if (o == null) return null;
System.IO.MemoryStream ms = null;
try
{
ms = new System.IO.MemoryStream();
var xmlserializer = GetXmlSerializerGoole(o.GetType());
xmlserializer.Serialize(ms, o, new System.Xml.Serialization.XmlSerializerNamespaces(new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", "") }));// (new System.Xml.XmlQualifiedName[] { new System.Xml.XmlQualifiedName("", "") }));
string xml = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
ms.Close(); ms.Dispose(); ms = null;
return xml.Replace("\0", "");
}
catch { return null; }
finally { if (ms != null) { ms.Close(); ms.Dispose(); } }
}
static System.Xml.Serialization.XmlSerializer GetXmlSerializerGoole(Type type)
{
System.Xml.Serialization.XmlSerializer ser = (System.Xml.Serialization.XmlSerializer)serializers[type];
if (ser == null)
{
ser = new System.Xml.Serialization.XmlSerializer(type, new System.Xml.Serialization.XmlRootAttribute() { ElementName = "urlset", Namespace = "" });
serializers[type] = ser;
}
return ser;
}
public static string Serializer(object obj)
{
try
{
MemoryStream stream = new MemoryStream();
System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = System.Text.Encoding.UTF8;
settings.IndentChars = " ";
//不生成声明头
settings.OmitXmlDeclaration = true;
//强制指定命名空间,覆盖默认的命名空间。
System.Xml.Serialization.XmlSerializerNamespaces namespaces = new System.Xml.Serialization.XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, settings))
{
//序列化对象
xml.Serialize(writer, obj, namespaces);
writer.Close();
}
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
string str = sr.ReadToEnd();
sr.Dispose();
stream.Dispose();
return str;
}
catch (Exception e)
{
return "";
}
}
public static T Deserialize(string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
System.Xml.Serialization.XmlSerializer xmldes = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
return default(T);
}
}
}
}