Convert Between Class And Xml
public T DeSerializer<T>(string serializer)
{
DataContractSerializer ser = new DataContractSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serializer));
T DatatractDto = (T)ser.ReadObject(ms);
ms.Close();
return DatatractDto;
}
public string Serialize<T>(T instance)
{
DataContractSerializer ser = new DataContractSerializer(instance.GetType());
using (MemoryStream ms = new MemoryStream())
{
ser.WriteObject(ms, instance);
StringBuilder sb = new StringBuilder();
sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
return sb.ToString();
}
}
Description:
DataContractSerializer Class from namespace of System.Runtime.Serialization
MemoryStream Class from namespace of System.IO
Import:
how to DeSerializer ? the xml string contains the namespace of T.It could based on the namespace to find T,then do the DeSerializer .So,if we change the namespace of T,it will not success because of no finding T by namespace!