首先我们需要明确两个概念:序列化和反序列化。序列化是将对象转换为XML或JSON格式的文本数据,而反序列化则是将XML或JSON格式的文本数据转换为对象。
在C#中,我们可以使用XmlSerializer类来实现XML和实体类之间的序列化和反序列化。以下是详细的步骤:
1. 定义实体类
我们先定义一个Person类来说明这个过程:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
2. 序列化
在代码中创建一个XmlSerializer实例,然后调用Serialize方法将Person对象转换成XML格式的文本:
Person person = new Person()
{
Name = "John Smith",
Age = 30,
Email = "john.smith@example.com"
};
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, person);
string result = writer.ToString();
Console.WriteLine(result);
}
这段代码将会输出以下XML格式的文本:
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>John Smith</Name>
<Age>30</Age>
<Email>john.smith@example.com</Email>
</Person>
3. 反序列化
反序列化的过程和序列化相反,我们需要使用XmlSerializer类的Deserialize方法将XML格式的文本转换成Person对象:
string xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Person xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Name>John Smith</Name>
<Age>30</Age>
<Email>john.smith@example.com</Email>
</Person>";
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (StringReader reader = new StringReader(xml))
{
Person person = (Person)serializer.Deserialize(reader);
Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
Console.WriteLine(person.Email);
}
这段代码将会输出以下结果:
John Smith
30
john.smith@example.com
示例1:序列化和反序列化List对象
上面只是展示了单个对象的序列化和反序列化,实际情况中我们需要序列化的是一个列表,下面给出一个使用List对象的示例。
List<Person> people = new List<Person>()
{
new Person() { Name = "John", Age = 30, Email = "john@example.com" },
new Person() { Name = "Jane", Age = 35, Email = "jane@example.com" },
};
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, people);
string result = writer.ToString();
Console.WriteLine(result);
}
// 反序列化
using (StringReader reader = new StringReader(result))
{
List<Person> deserializedPeople = (List<Person>)serializer.Deserialize(reader);
foreach (Person person in deserializedPeople)
{
Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
Console.WriteLine(person.Email);
}
}
示例2:自定义XML元素名称
有时候我们需要给XML元素指定特定的名称,而不是使用默认的属性名。这时候我们可以使用XmlAttribute或者XmlText特定来标记实体类属性。
public class PersonWithXmlAttribute
{
[XmlAttribute("FullName")]
public string Name { get; set; }
public int Age { get; set; }
[XmlText]
public string Email { get; set; }
}
我们可以使用上面的代码创建一个新的PersonWithXmlAttribute对象并将其序列化:
PersonWithXmlAttribute person = new PersonWithXmlAttribute()
{
Name = "John Smith",
Age = 30,
Email = "john.smith@example.com"
};
XmlSerializer serializer = new XmlSerializer(typeof(PersonWithXmlAttribute));
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, person);
string result = writer.ToString();
Console.WriteLine(result);
}
这段代码将会输出以下XML格式的文本:
<?xml version="1.0" encoding="utf-16"?>
<PersonWithXmlAttribute xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" FullName="John Smith">john.smith@example.com</PersonWithXmlAttribute>
反序列化同样的方法:
string xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<PersonWithXmlAttribute xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" FullName=""John Smith"">john.smith@example.com</PersonWithXmlAttribute>";
XmlSerializer serializer = new XmlSerializer(typeof(PersonWithXmlAttribute));
using (StringReader reader = new StringReader(xml))
{
PersonWithXmlAttribute deserializedPerson = (PersonWithXmlAttribute)serializer.Deserialize(reader);
Console.WriteLine(deserializedPerson.Name);
Console.WriteLine(deserializedPerson.Age);
Console.WriteLine(deserializedPerson.Email);
}
这段代码将会输出以下结果:
John Smith
0
john.smith@example.com
注意,因为Age属性没有使用标记,因此当它被反序列化时,它的值将保持为默认值0。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现XML与实体类之间相互转换的方法(序列化与反序列化) - Python技术站