C#|Xml 简单序列化和返序列化
结构体
和类
都能进行 xml 的序列化和反序列化。
必须包含的命名空间:
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
基本的序列化
这里以结构体
类型为例 :
public struct Student
{
public string Name;
public int Age;
public Student(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
这里访问类型必须要public
。
class XmlSerializerHelper
{
public void XmlSerial(object obj, string xmlFile)
{
// 序列化
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings setting = new XmlWriterSettings() {
Encoding = new UTF8Encoding(false),
Indent = true,
};
using (XmlWriter writer = XmlWriter.Create(ms, setting))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
// 去掉命名空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // 命名空间赋值为空
xmlSerializer.Serialize(writer, obj, ns);
// 用StreamWriter
string xmlContent = Encoding.UTF8.GetString(ms.ToArray()); // 转成字符转
StreamWriter streamWriter = new StreamWriter(xmlFile);
streamWriter.Write(xmlContent);
streamWriter.Close();
}
}
}
// 返序列化
public object XmlDeserial(object obj, string xmlFile)
{
// 用 MemoryStream
string xmlString = File.ReadAllText(xmlFile); // xml 内容提取出来
using (MemoryStream MS = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
using (XmlReader xr = XmlReader.Create(MS))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
obj = xmlSerializer.Deserialize(xr); // 不要误写为 MS 流,有这个重载不会报错。但运行有错。
}
}
return obj;
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student() { Name = "毛泽东", Age = 18 };
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
xmlSerializerHelper.XmlSerial(student, "Student.xml");
Console.ReadKey();
}
}
输出,没什么问题:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Name>毛泽东</Name>
<Age>18</Age>
</Student>
简单反序列化
class Program
{
static void Main(string[] args)
{
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
Student stu;
stu = (Student)xmlSerializerHelper.XmlDeserial(new Student(), "Student.xml");
Console.WriteLine("Name = {0}\nAge = {1}", stu.Name, stu.Age);
Console.ReadKey();
}
}
输出,没什么问题:
Name = 毛泽东
Age = 18
测试一下嵌套序列化
public struct Student
{
public string Name;
public int Age;
public OtherInfo OtherInfo;
}
public struct OtherInfo
{
public int Birth;
public string Alisa;
}
class Program
{
static void Main(string[] args)
{
OtherInfo otherInfo = new OtherInfo() { Birth = 1893, Alisa = "毛润之"};
Student student = new Student() { Name = "毛泽东", Age = 18, OtherInfo = otherInfo };
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
xmlSerializerHelper.XmlSerial(student, "Student.xml");
Console.ReadKey();
}
}
输出:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Name>毛泽东</Name>
<Age>18</Age>
<OtherInfo>
<Birth>1893</Birth>
<Alisa>毛润之</Alisa>
</OtherInfo>
</Student>
测试下如果引用变量为null的情况
class Program
{
static void Main(string[] args)
{
Student student = new Student() ;
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
xmlSerializerHelper.XmlSerial(student, "Student.xml");
Console.ReadKey();
}
}
输出:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Age>0</Age>
<OtherInfo>
<Birth>0</Birth>
</OtherInfo>
</Student>
可见,null值不进行序列化,int类型会默认为0,所以存在。
把struct
改成class
:
public class Student
{
public string Name;
public int Age;
public OtherInfo OtherInfo;
}
public class OtherInfo
{
public int Birth;
public string Alisa;
}
输出:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Age>0</Age>
</Student>
class
为引用类型,为 null
没输出了。
数组的序列化
输出:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
[XmlElement("Dog")] // 不要“再包一层”
public List<Dog> Dog { get; set; }
public string Good { get; set; } = "100";
private string valuePrivate { get; set; } = "0";
}
public class Dog
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Dog> dogs = new List<Dog> {
new Dog() { Name = "小黑", Age = 9 },
new Dog() { Name = "小黄", Age = 10 }
};
Person person = new Person() {
Name = "孙悟空",
Age = 500,
Sex = "男",
Dog = dogs
};
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
xmlSerializerHelper.XmlSerial(person, "Person.xml");
Console.ReadKey();
}
}
<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>孙悟空</Name>
<Age>500</Age>
<Sex>男</Sex>
<Dog>
<Name>小黑</Name>
<Age>9</Age>
</Dog>
<Dog>
<Name>小黄</Name>
<Age>10</Age>
</Dog>
<Good>100</Good>
</Person>
可见:
-
数组如果不要再包一层, [XmlElement("Dog")] // 不要“再包一层”
显示的命名个节点。不然输出如下:
<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>孙悟空</Name>
<Age>500</Age>
<Sex>男</Sex>
<Dog>
<Dog>
<Name>小黑</Name>
<Age>9</Age>
</Dog>
<Dog>
<Name>小黄</Name>
<Age>10</Age>
</Dog>
</Dog>
<Good>100</Good>
</Person>
Dog
数组外面包了一层<Dog>
。
-
private string valuePrivate { get; set; } = "0";
私有成员没有被序列化。
参考:
https://www.cnblogs.com/guogangj/p/7489218.html