XML与对象,集合的相互转化
今天小伙伴在群里问了一下关于XML与对象之间的相互转换,作为菜鸟的我正好趁着闲着的时间学习了一波,直接上代码了,有疑问或者有错误的地方还请大家指正,谢谢。。。。
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Xml.Serialization; 8 using System.Xml; 9 10 namespace xml序列化 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 17 var stu = new Student(); 18 stu.Name = "华哥"; 19 stu.Age = 18; 20 stu.Sex = "男神"; 21 var stu1 = new Student("xiuxiu", 18, "女"); 22 var stuList = new List<Student>(); 23 stuList.Add(stu); 24 stuList.Add(stu1); 25 26 var xmlStr = XmlConvert<Student>.SerializeObject(stu);//序列化 27 Console.WriteLine(xmlStr); 28 29 var student = XmlConvert<Student>.DeserializeObject(xmlStr);//反序列化 30 Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); 31 32 var listXml = XmlConvert<Student>.ListSerializeObject(stuList);//List序列化 33 Console.WriteLine(listXml); 34 35 var xmlList = XmlConvert<Student>.ListDeserializeObject(listXml);//反序列化 36 37 38 Console.ReadKey(); 39 } 40 41 public class XmlConvert<T> where T : new() 42 { 43 44 //序列化对象成xml字符串 45 public static string SerializeObject(T myObj) 46 { 47 var xmlStr = string.Empty; 48 if (myObj != null) 49 { 50 XmlSerializer xs = new XmlSerializer(typeof(T)); 51 using (var stringWriter = new StringWriter()) 52 { 53 xs.Serialize(stringWriter, myObj); 54 xmlStr = stringWriter + ""; 55 } 56 } 57 return xmlStr; 58 } 59 60 //序列化对象成xml字符串 61 public static string SerializeObject1(T Object) 62 { 63 var xmlStr = string.Empty; 64 if (Object != null) 65 { 66 XmlSerializer xs = new XmlSerializer(typeof(T));//初始化XmlSerializer对象 67 MemoryStream stream = new MemoryStream(); 68 XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); 69 //writer.Formatting = Formatting.None;// 不应用特殊的格式设置。这是默认值。 70 writer.Formatting = Formatting.Indented; //设置缩进。 71 xs.Serialize(writer, Object); 72 stream.Position = 0; // 获取或设置流中的当前位置。 必须设置,否则默认最后位置,读取不到流中的数据 73 using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8)) 74 { 75 string line; 76 while ((line = reader.ReadLine()) != null) 77 { 78 xmlStr += line; 79 } 80 } 81 writer.Close(); 82 } 83 return xmlStr; 84 } 85 86 //xml反序列化成对象 87 public static T DeserializeObject(string xml) 88 { 89 var t = default(T) 90 if (!string.IsNullOrEmpty(xml)) 91 { 92 var xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); 93 StringReader reader = new StringReader(xml);//将xml字符串转换成stringreader 94 t = (T)xs.Deserialize(reader); 95 reader.Close(); 96 } 97 return t; 98 } 99 100 //序列化List成xml字符串 101 public static string ListSerializeObject(List<T> list) 102 { 103 var xmlStr = string.Empty; 104 if (list.Count > 0) 105 { 106 using (StringWriter sw = new StringWriter()) 107 { 108 XmlSerializer xz = new XmlSerializer(list.GetType()); 109 xz.Serialize(sw, list); 110 xmlStr = sw + ""; 111 } 112 } 113 return xmlStr; 114 } 115 116 //xml反序列化成List集合 117 public static List<T> ListDeserializeObject(string xml) 118 { 119 List<T> t =null; 120 if (!string.IsNullOrEmpty(xml)) 121 { 122 t = new List<T>(); 123 XmlSerializer xs = new XmlSerializer(typeof(List<T>)); 124 using (var sw = new StringReader(xml)) 125 { 126 t = (List<T>)xs.Deserialize(sw); 127 } 128 } 129 return t; 130 } 131 } 132 } 133 public class Student 134 { 135 public Student() { } 136 public Student(string name, int age, string sex) 137 { 138 Name = name; 139 Age = age; 140 Sex = sex; 141 } 142 public string Name { get; set; } 143 public int? Age { get; set; } 144 public string Sex { get; set; } 145 } 146 }
根据JsonConvert类的习惯,改良了一下XMLConvert类,见如下代码
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Xml.Serialization; 8 using System.Xml; 9 10 namespace xml序列化 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 17 var stu = new Student(); 18 stu.Name = "华哥"; 19 stu.Age = 18; 20 stu.Sex = "男神"; 21 var stu1 = new Student("xiuxiu", 18, "女"); 22 var stuList = new List<Student>(); 23 stuList.Add(stu); 24 stuList.Add(stu1); 25 26 var xmlStr = XmlConvert.SerializeObject(stu);//序列化 27 Console.WriteLine(xmlStr); 28 29 var student = XmlConvert.DeserializeObject<Student>("");//反序列化 30 Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); 31 32 var listXml = XmlConvert.SerializeObject(stuList);//List序列化 33 Console.WriteLine(listXml); 34 35 var xmlList = XmlConvert.ListDeserializeObject<Student>(listXml);//反序列化 36 37 38 Console.ReadKey(); 39 } 40 41 public class XmlConvert 42 { 43 44 //序列化对象成xml字符串 45 public static string SerializeObject(object myObj) 46 { 47 var xmlStr = string.Empty; 48 if (myObj != null) 49 { 50 XmlSerializer xs = new XmlSerializer(myObj.GetType()); 51 using (var stringWriter = new StringWriter()) 52 { 53 xs.Serialize(stringWriter, myObj); 54 xmlStr = stringWriter + ""; 55 } 56 } 57 return xmlStr; 58 } 59 60 //序列化对象成xml字符串 61 public static string SerializeObject1(object Object) 62 { 63 var xmlStr = string.Empty; 64 if (Object != null) 65 { 66 XmlSerializer xs = new XmlSerializer(Object.GetType());//初始化XmlSerializer对象 67 MemoryStream stream = new MemoryStream(); 68 XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); 69 //writer.Formatting = Formatting.None;// 不应用特殊的格式设置。这是默认值。 70 writer.Formatting = Formatting.Indented; //设置缩进。 71 xs.Serialize(writer, Object); 72 stream.Position = 0; // 获取或设置流中的当前位置。 必须设置,否则默认最后位置,读取不到流中的数据 73 using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8)) 74 { 75 string line; 76 while ((line = reader.ReadLine()) != null) 77 { 78 xmlStr += line; 79 } 80 } 81 writer.Close(); 82 } 83 return xmlStr; 84 } 85 86 //xml反序列化成对象 87 public static T DeserializeObject<T>(string xml) where T : new() 88 { 89 T t = default(T); 90 if (!string.IsNullOrEmpty(xml)) 91 { 92 t = new T(); 93 var xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); 94 StringReader reader = new StringReader(xml);//将xml字符串转换成stream 95 t = (T)xs.Deserialize(reader); 96 reader.Close(); 97 } 98 return t; 99 } 100 101 //序列化List成xml字符串 102 public static string ListSerializeObject(object list) 103 { 104 var xmlStr = string.Empty; 105 if (list != null) 106 { 107 using (StringWriter sw = new StringWriter()) 108 { 109 XmlSerializer xz = new XmlSerializer(list.GetType()); 110 xz.Serialize(sw, list); 111 xmlStr = sw + ""; 112 } 113 } 114 return xmlStr; 115 } 116 117 //xml反序列化成List集合 118 public static List<T> ListDeserializeObject<T>(string xml) where T : new() 119 { 120 List<T> t = null; 121 if (!string.IsNullOrEmpty(xml)) 122 { 123 t = new List<T>(); 124 XmlSerializer xs = new XmlSerializer(typeof(List<T>)); 125 using (var sw = new StringReader(xml)) 126 { 127 t = (List<T>)xs.Deserialize(sw); 128 } 129 } 130 return t; 131 } 132 } 133 } 134 public class Student 135 { 136 public Student() { } 137 public Student(string name, int age, string sex) 138 { 139 Name = name; 140 Age = age; 141 Sex = sex; 142 } 143 public string Name { get; set; } 144 public int? Age { get; set; } 145 public string Sex { get; set; } 146 } 147 }
进一步的简化,直接跟JsonConvert类的方法一致。。。。。
1 using System.Linq; 2 using System.Text; 3 using System.Threading.Tasks; 4 using System.Xml.Serialization; 5 using System.Xml; 6 7 namespace xml序列化 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 var stu = new Student(); 15 stu.Name = "华哥"; 16 stu.Age = 18; 17 stu.Sex = "男神"; 18 var stu1 = new Student("xiuxiu", 18, "女"); 19 var stuList = new List<Student>(); 20 stuList.Add(stu); 21 stuList.Add(stu1); 22 23 var xmlStr = XmlConvert.SerializeObject(stu);//序列化 24 Console.WriteLine(xmlStr); 25 26 var student = XmlConvert.DeserializeObject<Student>("");//反序列化 27 Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); 28 29 var listXml = XmlConvert.SerializeObject(stuList);//List序列化 30 Console.WriteLine(listXml); 31 32 var xmlList = XmlConvert.DeserializeObject<List<Student>>(listXml);//反序列化 33 34 35 Console.ReadKey(); 36 } 37 public class XmlConvert 38 { 39 //序列化对象成xml字符串 40 public static string SerializeObject(object myObj) 41 { 42 var xmlStr = string.Empty; 43 if (myObj != null) 44 { 45 XmlSerializer xs = new XmlSerializer(myObj.GetType()); 46 using (var stringWriter = new StringWriter()) 47 { 48 xs.Serialize(stringWriter, myObj); 49 xmlStr = stringWriter + ""; 50 } 51 } 52 return xmlStr; 53 } 54 55 //xml反序列化成对象 56 public static T DeserializeObject<T>(string xml) where T : new() 57 { 58 T t = default(T); 59 if (!string.IsNullOrEmpty(xml)) 60 { 61 t = new T(); 62 var xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); 63 StringReader reader = new StringReader(xml);//将xml字符串转换成stream 64 t = (T)xs.Deserialize(reader); 65 reader.Close(); 66 } 67 return t; 68 } 69 } 70 } 71 public class Student 72 { 73 public Student() { } 74 public Student(string name, int age, string sex) 75 { 76 Name = name; 77 Age = age; 78 Sex = sex; 79 } 80 public string Name { get; set; } 81 public int? Age { get; set; } 82 public string Sex { get; set; } 83 }