1. 序列化代码:
static string SerializeData<T>(T data)
{
var serializer = new DataContractSerializer(typeof(T));
using (var stringWriter = new System.IO.StringWriter())
using (var writer = new System.Xml.XmlTextWriter(stringWriter))
{
serializer.WriteObject(writer, data);
return(stringWriter.ToString());
}
}
2. 反序列化代码:
static T DeserializeData<T>(string serializedString)
{
var serializer = new DataContractSerializer(typeof(T));
using (var stringReader = new System.IO.StringReader(serializedString))
using (var reader = new System.Xml.XmlTextReader(stringReader))
{
return (T)(serializer.ReadObject(reader));
}
}
3. 测试
3.1 测试的数据类型:
[DataContract(Namespace = "http://test.com")]
public class TestData
{