using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class XMLHelper
{
#region xml serializtion;
#region serailize
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string XmlSerialize(Object obj, Encoding encoding)
{
return XmlSerialize(obj, encoding, false);
}
public static string XmlSerialize(Object obj, Encoding encoding, bool writeFullTag)
{
#region set settings
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
settings.IndentChars = string.Empty;
settings.NewLineChars = Environment.NewLine;
settings.Encoding = encoding;
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
settings.ConformanceLevel = ConformanceLevel.Auto;
#endregion
return XmlSerialize(obj, settings, encoding, writeFullTag);
}
public static string XmlSerialize(Object obj, XmlWriterSettings settings, Encoding encoding, bool writeFullTag)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlWriter writer = null;
using (Stream stream = new MemoryStream())
{
if (writeFullTag)
{
writer = new XmlTextWriterFull(stream, encoding);
}
else
{
writer = XmlWriter.Create(stream, settings);
}
using (StreamReader reader = new StreamReader(stream))
{
serializer.Serialize(writer, obj, ns);
stream.Position = 0;
return reader.ReadToEnd();
}
}
}
#endregion serailize
/// <summary>
/// 把对象序列化并保存到文件.
/// </summary>
/// <param name="obj">对象</param>
/// <param name="path">要保存的文件路径</param>
/// <param name="encoding">编码方式</param>
public static void XmlSerializeToFile(object obj, string path, Encoding encoding)
{
#region check arguments
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
#endregion
File.WriteAllText(path, XmlSerialize(obj, encoding, true), encoding);
}
#region deserialize
/// <summary>
/// 从XML字符串中反序列化对象
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="s">包含对象的XML字符串</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserialize<T>(string xmlStrings, Encoding encoding)
{
#region check arguments
if (string.IsNullOrEmpty(xmlStrings))
throw new ArgumentNullException("xmlStrings");
if (encoding == null)
throw new ArgumentNullException("encoding");
#endregion
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using (MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(xmlStrings)))
{
using (StreamReader streamReader = new StreamReader(memoryStream, encoding))
{
return (T)mySerializer.Deserialize(streamReader);
}
}
}
/// <summary>
/// 对一个文件中的XML字符串反序列化。
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="path">文件路径</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserializeFromFile<T>(string path, Encoding encoding)
{
#region check arguments
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
if (encoding == null)
throw new ArgumentNullException("encoding");
#endregion
string xmlStrings = File.ReadAllText(path, encoding);
return XmlDeserialize<T>(xmlStrings, encoding);
}
#endregion deserialize
#endregion
}
public class XmlTextWriterFull : XmlTextWriter
{
public XmlTextWriterFull(TextWriter w)
: base(w)
{
}
public XmlTextWriterFull(Stream stream, Encoding encoding)
: base(stream, encoding)
{
}
public XmlTextWriterFull(string fileName, Encoding encoding)
: base(fileName, encoding)
{
}
public override void WriteEndElement()
{
base.WriteFullEndElement();
}
}
注:1)XmlTextWriterFull这个类为了实现序列化xml空节点为<node1></node1>这样的格式。
2) XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
settings.IndentChars = string.Empty;
settings.NewLineChars = Environment.NewLine;
settings.Encoding = encoding;
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
settings.ConformanceLevel = ConformanceLevel.Auto;
这段代码可以实现xml字符串的相关格式。
3) XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
这段代码可以去掉生成XML文件的默认的命名空间。
申明:部分方法参考网上相关资源。