public class Class2
{
[XmlAttribute]
public int IntValue { get; set; }
[XmlElement]
public string StrValue { get; set; }
}
public class Class3
{
[XmlAttribute]
public int IntValue { get; set; }
[XmlText]
public string StrValue { get; set; }
}
[XmlType("c4")]
public class Class4
{
[XmlAttribute("id")]
public int IntValue { get; set; }
[XmlElement("name")]
public string StrValue { get; set; }
}
[XmlRoot("c4List")]
public class Class4List : List<Class4> { }
public class Root
{
public Class3 Class3 { get; set; }
[XmlArrayItem("c2")]
[XmlArray("cccccccccccc")]
public List<Class2> List { get; set; }
}
public class Root
{
public Class3 Class3 { get; set; }
[XmlElement("c2")]
public List<Class2> List { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<XRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<List>
<x1 aa="1" bb="2" />
<x1 aa="3" bb="4" />
<x2>
<cc>ccccccccccc</cc>
<dd>dddddddddddd</dd>
</x2>
</List>
</XRoot>
public class XBase { }
[XmlType("x1")]
public class X1 : XBase
{
[XmlAttribute("aa")]
public int AA { get; set; }
[XmlAttribute("bb")]
public int BB { get; set; }
}
[XmlType("x2")]
public class X2 : XBase
{
[XmlElement("cc")]
public string CC { get; set; }
[XmlElement("dd")]
public string DD { get; set; }
}
public class XRoot
{
[XmlArrayItem(typeof(X1)),
XmlArrayItem(typeof(X2))]
public List<XBase> List { get; set; }
}
public class TestIgnore
{
[XmlIgnore] // 这个属性将不会参与序列化
public int IntValue { get; set; }
public string StrValue { get; set; }
public string Url;
}
public class TestIgnore
{
[XmlElement(Order = 1)]
public string StrValue { get; set; }
[XmlElement(Order = 2)]
public string Url;
}
// 1. 首先要创建或者得到一个数据对象
Order order = GetOrderById(123);
// 2. 用序列化的方法生成XML
string xml = XmlHelper.XmlSerialize(order, Encoding.UTF8);
// 3. 从XML读取数据并生成对象
Order order2 = XmlHelper.XmlDeserialize<Order>(xml, Encoding.UTF8);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.IO; using System.Xml;
public static class XmlHelper {
/// <summary>
/// 将一个对象序列化
/// </summary>
private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
{
if( o == null )
throw new ArgumentNullException("o");
if( encoding == null )
throw new ArgumentNullException("encoding");
XmlSerializer serializer = new XmlSerializer(o.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = " ";
using( XmlWriter writer = XmlWriter.Create(stream, settings) ) {
serializer.Serialize(writer, o);
writer.Close();
}
}
/// <summary>
/// 将一个对象序列化为XML字符串
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>序列化产生的XML字符串</returns>
public static string XmlSerialize(object o, Encoding encoding)
{
using( MemoryStream stream = new MemoryStream() ) {
XmlSerializeInternal(stream, o, encoding);
stream.Position = 0;
using( StreamReader reader = new StreamReader(stream, encoding) ) {
return reader.ReadToEnd();
}
}
}
/// <summary>
/// 将一个对象按XML序列化的方式写入到一个文件
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <param name="path">保存文件路径</param>
/// <param name="encoding">编码方式</param>
public static void XmlSerializeToFile(object o, string path, Encoding encoding)
{
if( string.IsNullOrEmpty(path) )
throw new ArgumentNullException("path");
using( FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write) ) {
XmlSerializeInternal(file, o, encoding);
}
}
/// <summary>
/// 从XML字符串中反序列化对象
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="s">包含对象的XML字符串</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserialize<T>(string s, Encoding encoding)
{
if( string.IsNullOrEmpty(s) )
throw new ArgumentNullException("s");
if( encoding == null )
throw new ArgumentNullException("encoding");
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using( MemoryStream ms = new MemoryStream(encoding.GetBytes(s)) ) {
using( StreamReader sr = new StreamReader(ms, encoding) ) {
return (T)mySerializer.Deserialize(sr);
}
}
}
/// <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)
{
if( string.IsNullOrEmpty(path) )
throw new ArgumentNullException("path");
if( encoding == null )
throw new ArgumentNullException("encoding");
string xml = File.ReadAllText(path, encoding);
return XmlDeserialize<T>(xml, encoding);
}
}