下面请认真阅读以下完整攻略。
c#正反序列化XML文件示例(xml序列化)
什么是XML序列化?
XML序列化是一种将对象统一保存为XML格式并在需要时将XML信息还原成对象的技术。在C#中,可以使用System.Xml.Serialization
命名空间中的类来实现XML序列化和反序列化。
XML序列化的基本过程
1. 定义要序列化的对象
在C#中,需要将一个对象序列化为XML格式,首先需要定义这个对象,并在类声明处添加[Serializable]
标记,如下所示:
[Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
2. 创建XmlSerializer对象
创建XmlSerializer
对象时需要传入要序列化的对象类型作为参数。
XmlSerializer serializer = new XmlSerializer(typeof(Student));
3. 将对象序列化为XML文件
通过XmlSerializer
对象的Serialize
方法将要序列化的对象转换为XML格式并保存到文件中。
Student student = new Student() { Name = "Tom", Age = 18, Gender = "Male" };
using (FileStream fs = new FileStream("student.xml", FileMode.Create))
{
serializer.Serialize(fs, student);
}
4. 将XML文件反序列化为对象
通过XmlSerializer
对象的Deserialize
方法,将XML文件还原成对象。
using (FileStream fs = new FileStream("student.xml", FileMode.Open))
{
Student deserializedStudent = (Student)serializer.Deserialize(fs);
Console.WriteLine($"Name:{deserializedStudent.Name}, Age:{deserializedStudent.Age}, Gender:{deserializedStudent.Gender}");
}
示例1:将List序列化为XML文件并读取
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
class Program
{
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Student>));
List<Student> students = new List<Student>()
{
new Student(){Name="Tom",Age=18,Gender="Male"},
new Student(){Name="Lucy",Age=17,Gender="Female"}
};
//序列化为XML文件并保存
using (FileStream fs = new FileStream("students.xml", FileMode.Create))
{
serializer.Serialize(fs, students);
}
//从XML文件中读取序列化的数据并反序列化为对象列表
using (FileStream fs = new FileStream("students.xml", FileMode.Open))
{
List<Student> deserializedStudents = (List<Student>)serializer.Deserialize(fs);
foreach (var student in deserializedStudents)
{
Console.WriteLine($"Name:{student.Name}, Age:{student.Age}, Gender:{student.Gender}");
}
}
}
}
示例2:使用XmlWriter和XmlReader实现XML序列化
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
[Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(){Name="Tom",Age=18,Gender="Male"},
new Student(){Name="Lucy",Age=17,Gender="Female"}
};
//将对象序列化为XML文本
using (TextWriter writer = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Student>));
serializer.Serialize(writer, students);
Console.WriteLine(writer.ToString());
}
//将XML文本反序列化为对象列表
using (TextReader reader = new StringReader("<?xml version=\"1.0\" encoding=\"utf-16\" ?><ArrayOfStudent xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Student><Name>Tom</Name><Age>18</Age><Gender>Male</Gender></Student><Student><Name>Lucy</Name><Age>17</Age><Gender>Female</Gender></Student></ArrayOfStudent>"))
{
List<Student> deserializedStudents = new List<Student>();
XmlReader xmlReader = XmlReader.Create(reader);
while (xmlReader.Read())
{
if (xmlReader.Name == "Student" && xmlReader.IsStartElement())
{
Student student = new Student()
{
Name = xmlReader.GetAttribute("Name"),
Age = int.Parse(xmlReader.GetAttribute("Age")),
Gender = xmlReader.GetAttribute("Gender")
};
deserializedStudents.Add(student);
}
}
foreach (var student in deserializedStudents)
{
Console.WriteLine($"Name:{student.Name}, Age:{student.Age}, Gender:{student.Gender}");
}
}
}
}
以上就是关于c#正反序列化XML文件的示例和攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:c#正反序列化XML文件示例(xml序列化) - Python技术站