Simple functions, for custom types, list And array implement serialization.
It contains (reflection creates an array, reflection creates a wildcard, reflection fills the generic content, reflection fills the content of the array) In fact, I write this section to let you come.
Public static class serializexmlhelper {////// Serialize an object (the non-list is XML )////////////
Public static xelement serializetoxml (this object OBJ, string rootname = NULL) {var T = obj. getType (); xelement root; If (T. isgenerictype) {root = obj. serializelisttoxml (rootname);} else if (T. isarray) {root = obj. serializearraytoxml (rootname);} else {root = new xelement (T. name); var PS = T. getproperties (); If (PS. length = 0) {root. value = obj. tostring ();} else {foreach (var p in PS) {If (P. propertytype. basetype = typeof (object) {var node = new xelement (P. name, P. getvalue (OBJ, null); root. add (node);} else root. add (P. getvalue (OBJ, null ). serializetoxml (P. name) ;}}return root ;}////// Serialize a list as XML ////////////
Public static xelement serializelisttoxml (this object OBJ, string rootname) {var root = new xelement (rootname); var list = (ienumerable) OBJ; foreach (var x in list) {root. add (X. serializetoxml (rootname);} return root ;}////// Serialize an array as XML ////////////
Public static xelement serializearraytoxml (this object OBJ, string rootname) {var root = new xelement (rootname); foreach (VAR o in (array) OBJ) {root. add (O. serializetoxml (rootname);} return root ;}////// Reverse Sequence XML is an object //////
//////
Public static t deserializefromxml
(This xelement element) {return (t) deserializefromxml (element, typeof (t);} public static object deserializefromxml (this xelement element, type T) {// if it is inherited, the namespace must be consistent. If (T. Name! = Element. name) {T = type. getType (T. namespace + ". "+ element. name); If (t = NULL) return NULL;} var OBJ = activator. createinstance (t); var PS = T. getproperties (); foreach (var p in PS) {var E = element. element (P. name); If (! E. haselements) {P. setvalue (OBJ, E. value, null);} else {If (P. propertytype. isarray) {var TMP = deserializearrayfromxml (E, P. propertytype); p. setvalue (OBJ, TMP, null);} else if (P. propertytype. isgenerictype) {P. setvalue (OBJ, deserializelistfromxml (E, P. propertytype), null);} else {P. setvalue (OBJ, E. value, null) ;}} return OBJ ;}///
/// Deserialize XML into an array /////////
Public static T [] deserializearrayfromxml
(This xcontainer element) {return (T []) deserializearrayfromxml (element, typeof (t);} public static array deserializearrayfromxml (this xcontainer element, type T) {var elements = element. elements (); // create an array var Atype = type. getType (T. fullname. replace ("[]", ""); var array = array. createinstance (Atype, elements. count (); var I = 0; foreach (VAR e in elements) {If (E. haselements) {array. setvalue (deserializefromxml (E, Atype), I);} else {array. setvalue (convert. changetype (E. value, Atype), I);} I ++;} return array ;}///
/// Deserialize XML into a generic listt /////////
Public static t deserializelistfromxml
(This xcontainer element) {return (t) deserializelistfromxml (element, typeof (t ));}///
/// Deserialize XML into a generic listt //////// Type of the generic list ///
Public static ienumerable deserializelistfromxml (this xcontainer element, type T) {var elements = element. elements (); t = type. getType (T. fullname. replace ("ienumerable", "list"); var list = (ienumerable) activator. createinstance (t); var argt = T. getgenericarguments () [0]; var add = T. getmethod ("add", new [] {argt}); foreach (VAR e in elements) {// if it is a custom type if (E. haselements) {Add. invoke (list, new [] {deserializefromxml (E, argt)});} else add. invoke (list, new [] {convert. changetype (E. value, argt)}) ;}return list ;}}
Test code:
namespace ConsoleApplication2
{
public class A
{
public string Name { get; set; }
}
public class Test
{
public string Name { get; set; }
public int[] Arr { get; set; }
public A[] Arr1 { get; set; }
public IEnumerable<A> List { get; set; }
}
class Program
{
static void Main(string[] args)
{
var t = new Test
{
Name = "T1",
Arr = new[] { 1, 2, 3, 4 },
Arr1 = new [] { new A { Name = "A1" }, new A { Name = "A2" } },
List = new List<A> { new A { Name = "A1" }, new A { Name = "A2" } }
};
var xml = t.SerializeToXml();
Console.WriteLine(xml);
var obj = new Test();
obj.DeserializeFromXml(xml);
Console.WriteLine(obj.Arr[0]);
Console.Read();
}
public static string XmlSerializer<T>(T serialObject) where T : class
{
var ser = new XmlSerializer(serialObject.GetType());
var mem = new MemoryStream();
var writer = new XmlTextWriter(mem, Encoding.UTF8);
ser.Serialize(writer, serialObject);
writer.Close();
return Encoding.UTF8.GetString(mem.ToArray());
}
}
}
Running result: