XML与对象,集合的相互转化

  今天小伙伴在群里问了一下关于XML与对象之间的相互转换,作为菜鸟的我正好趁着闲着的时间学习了一波,直接上代码了,有疑问或者有错误的地方还请大家指正,谢谢。。。。

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml; namespace xml序列化
{
class Program
{
static void Main(string[] args)
{ var stu = new Student();
stu.Name = "华哥";
stu.Age = ;
stu.Sex = "男神";
var stu1 = new Student("xiuxiu", , "女");
var stuList = new List<Student>();
stuList.Add(stu);
stuList.Add(stu1); var xmlStr = XmlConvert<Student>.SerializeObject(stu);//序列化
Console.WriteLine(xmlStr); var student = XmlConvert<Student>.DeserializeObject(xmlStr);//反序列化
Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); var listXml = XmlConvert<Student>.ListSerializeObject(stuList);//List序列化
Console.WriteLine(listXml); var xmlList = XmlConvert<Student>.ListDeserializeObject(listXml);//反序列化 Console.ReadKey();
} public class XmlConvert<T> where T : new()
{ //序列化对象成xml字符串
public static string SerializeObject(T myObj)
{
var xmlStr = string.Empty;
if (myObj != null)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (var stringWriter = new StringWriter())
{
xs.Serialize(stringWriter, myObj);
xmlStr = stringWriter + "";
}
}
return xmlStr;
} //序列化对象成xml字符串
public static string SerializeObject1(T Object)
{
var xmlStr = string.Empty;
if (Object != null)
{
XmlSerializer xs = new XmlSerializer(typeof(T));//初始化XmlSerializer对象
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
//writer.Formatting = Formatting.None;// 不应用特殊的格式设置。这是默认值。
writer.Formatting = Formatting.Indented; //设置缩进。
xs.Serialize(writer, Object);
stream.Position = ; // 获取或设置流中的当前位置。 必须设置,否则默认最后位置,读取不到流中的数据
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
xmlStr += line;
}
}
writer.Close();
}
return xmlStr;
} //xml反序列化成对象
public static T DeserializeObject(string xml)
{
var t = default(T)
if (!string.IsNullOrEmpty(xml))
{
var xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
StringReader reader = new StringReader(xml);//将xml字符串转换成stringreader
t = (T)xs.Deserialize(reader);
reader.Close();
}
return t;
} //序列化List成xml字符串
public static string ListSerializeObject(List<T> list)
{
var xmlStr = string.Empty;
if (list.Count > )
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(list.GetType());
xz.Serialize(sw, list);
xmlStr = sw + "";
}
}
return xmlStr;
} //xml反序列化成List集合
public static List<T> ListDeserializeObject(string xml)
{
List<T> t =null;
if (!string.IsNullOrEmpty(xml))
{
t = new List<T>();
XmlSerializer xs = new XmlSerializer(typeof(List<T>));
using (var sw = new StringReader(xml))
{
t = (List<T>)xs.Deserialize(sw);
}
}
return t;
}
}
}
public class Student
{
public Student() { }
public Student(string name, int age, string sex)
{
Name = name;
Age = age;
Sex = sex;
}
public string Name { get; set; }
public int? Age { get; set; }
public string Sex { get; set; }
}
}

  根据JsonConvert类的习惯,改良了一下XMLConvert类,见如下代码

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml; namespace xml序列化
{
class Program
{
static void Main(string[] args)
{ var stu = new Student();
stu.Name = "华哥";
stu.Age = ;
stu.Sex = "男神";
var stu1 = new Student("xiuxiu", , "女");
var stuList = new List<Student>();
stuList.Add(stu);
stuList.Add(stu1); var xmlStr = XmlConvert.SerializeObject(stu);//序列化
Console.WriteLine(xmlStr); var student = XmlConvert.DeserializeObject<Student>("");//反序列化
Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); var listXml = XmlConvert.SerializeObject(stuList);//List序列化
Console.WriteLine(listXml); var xmlList = XmlConvert.ListDeserializeObject<Student>(listXml);//反序列化 Console.ReadKey();
} public class XmlConvert
{ //序列化对象成xml字符串
public static string SerializeObject(object myObj)
{
var xmlStr = string.Empty;
if (myObj != null)
{
XmlSerializer xs = new XmlSerializer(myObj.GetType());
using (var stringWriter = new StringWriter())
{
xs.Serialize(stringWriter, myObj);
xmlStr = stringWriter + "";
}
}
return xmlStr;
} //序列化对象成xml字符串
public static string SerializeObject1(object Object)
{
var xmlStr = string.Empty;
if (Object != null)
{
XmlSerializer xs = new XmlSerializer(Object.GetType());//初始化XmlSerializer对象
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
//writer.Formatting = Formatting.None;// 不应用特殊的格式设置。这是默认值。
writer.Formatting = Formatting.Indented; //设置缩进。
xs.Serialize(writer, Object);
stream.Position = ; // 获取或设置流中的当前位置。 必须设置,否则默认最后位置,读取不到流中的数据
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
xmlStr += line;
}
}
writer.Close();
}
return xmlStr;
} //xml反序列化成对象
public static T DeserializeObject<T>(string xml) where T : new()
{
T t = default(T);
if (!string.IsNullOrEmpty(xml))
{
t = new T();
var xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
StringReader reader = new StringReader(xml);//将xml字符串转换成stream
t = (T)xs.Deserialize(reader);
reader.Close();
}
return t;
} //序列化List成xml字符串
public static string ListSerializeObject(object list)
{
var xmlStr = string.Empty;
if (list != null)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(list.GetType());
xz.Serialize(sw, list);
xmlStr = sw + "";
}
}
return xmlStr;
} //xml反序列化成List集合
public static List<T> ListDeserializeObject<T>(string xml) where T : new()
{
List<T> t = null;
if (!string.IsNullOrEmpty(xml))
{
t = new List<T>();
XmlSerializer xs = new XmlSerializer(typeof(List<T>));
using (var sw = new StringReader(xml))
{
t = (List<T>)xs.Deserialize(sw);
}
}
return t;
}
}
}
public class Student
{
public Student() { }
public Student(string name, int age, string sex)
{
Name = name;
Age = age;
Sex = sex;
}
public string Name { get; set; }
public int? Age { get; set; }
public string Sex { get; set; }
}
}

  进一步的简化,直接跟JsonConvert类的方法一致。。。。。

 using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml; namespace xml序列化
{
class Program
{
static void Main(string[] args)
{ var stu = new Student();
stu.Name = "华哥";
stu.Age = ;
stu.Sex = "男神";
var stu1 = new Student("xiuxiu", , "女");
var stuList = new List<Student>();
stuList.Add(stu);
stuList.Add(stu1); var xmlStr = XmlConvert.SerializeObject(stu);//序列化
Console.WriteLine(xmlStr); var student = XmlConvert.DeserializeObject<Student>("");//反序列化
Console.WriteLine(student.Name + "," + student.Age + "," + student.Sex); var listXml = XmlConvert.SerializeObject(stuList);//List序列化
Console.WriteLine(listXml); var xmlList = XmlConvert.DeserializeObject<List<Student>>(listXml);//反序列化 Console.ReadKey();
}
   public class XmlConvert
{
    //序列化对象成xml字符串
public static string SerializeObject(object myObj)
{
var xmlStr = string.Empty;
if (myObj != null)
{
XmlSerializer xs = new XmlSerializer(myObj.GetType());
using (var stringWriter = new StringWriter())
{
xs.Serialize(stringWriter, myObj);
xmlStr = stringWriter + "";
}
}
return xmlStr;
} //xml反序列化成对象
public static T DeserializeObject<T>(string xml) where T : new()
{
T t = default(T);
if (!string.IsNullOrEmpty(xml))
{
t = new T();
var xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
StringReader reader = new StringReader(xml);//将xml字符串转换成stream
t = (T)xs.Deserialize(reader);
reader.Close();
}
return t;
}
}
}
public class Student
{
public Student() { }
public Student(string name, int age, string sex)
{
Name = name;
Age = age;
Sex = sex;
}
public string Name { get; set; }
public int? Age { get; set; }
public string Sex { get; set; }
}
05-17 23:22