呵呵... 直接放代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
public class SimpleSerializer
{
/**//// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">对象</param>
/// <returns></returns>
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
/**//// <summary>
/// 反序列化为对象
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="s">对象序列化后的Xml字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string s)
{
using (StringReader sr = new StringReader(s))
{
XmlSerializer xz = new XmlSerializer(type);
return xz.Deserialize(sr);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
public class SimpleSerializer
{
/**//// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">对象</param>
/// <returns></returns>
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
/**//// <summary>
/// 反序列化为对象
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="s">对象序列化后的Xml字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string s)
{
using (StringReader sr = new StringReader(s))
{
XmlSerializer xz = new XmlSerializer(type);
return xz.Deserialize(sr);
}
}
}
}
申明数据对象,在这里大家是不是和我一样,数据一旦多了,就要老是写get,set很烦
有个简单的方法可以轻松实现,
首先我们申明 private 类型,然后在成员上单击鼠标右键->Refactor-> encapsulate field ... 然后系统会自动生成相应的get,set方法
[Serializable]
public class StockTicker
{
private string _instrumentType;
[XmlAttribute]
public string InstrumentType
{
get { return _instrumentType; }
set { _instrumentType = value; }
}
private string _userSymbol;
[XmlAttribute]
public string UserSymbol
{
get { return _userSymbol; }
set { _userSymbol = value; }
}
private int _seq;
[XmlAttribute]
public int Seq
{
get { return _seq; }
set { _seq = value; }
}
private int _msgType;
[XmlAttribute]
public int MsgType
{
get { return _msgType; }
set { _msgType = value; }
}
private int _bidSize;
[XmlAttribute]
public int BidSize
{
get { return _bidSize; }
set { _bidSize = value; }
}
private int _askSize;
[XmlAttribute]
public int AskSize
{
get { return _askSize; }
set { _askSize = value; }
}
}
public class StockTicker
{
private string _instrumentType;
[XmlAttribute]
public string InstrumentType
{
get { return _instrumentType; }
set { _instrumentType = value; }
}
private string _userSymbol;
[XmlAttribute]
public string UserSymbol
{
get { return _userSymbol; }
set { _userSymbol = value; }
}
private int _seq;
[XmlAttribute]
public int Seq
{
get { return _seq; }
set { _seq = value; }
}
private int _msgType;
[XmlAttribute]
public int MsgType
{
get { return _msgType; }
set { _msgType = value; }
}
private int _bidSize;
[XmlAttribute]
public int BidSize
{
get { return _bidSize; }
set { _bidSize = value; }
}
private int _askSize;
[XmlAttribute]
public int AskSize
{
get { return _askSize; }
set { _askSize = value; }
}
}