阅读目录
一:前言
二:实现步骤
三:运行效果
一:前言
之前我在一家电子商务公司做接口对接工作,同步快递公司给我们网站的订单数据,在这里我先给出快递公司给我的接口文档(见下图),我当时的做法是通过XmlDocument以及XmlElement,XmlNodeList等类去解析这个文档的,然后找到我要的数据更新至我们的数据库的,其实在这里有个更简单的做法,就是反序列化,把XML文档反序列化为对象。
二:实现步骤
1:XML文件编写
我们以学生科目为例
1.1 Student.xml
<?xml version="1.0" encoding="utf-8" ?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- 姓名-->
<Name>小强</Name>
<!-- 性别-->
<Sex>男</Sex>
<!-- 年龄-->
<Age>20</Age>
<!-- 课程集合-->
<Courses>
<!-- 课程-->
<Course>
<!-- 课程名称-->
<CourseName>英语</CourseName>
<!-- 课程分数-->
<Grade>92</Grade>
<!-- 课程描述-->
<CourseDescription>英语是一门全球应用最广泛的语言</CourseDescription>
</Course>
<Course>
<CourseName>数学</CourseName>
<Grade>98</Grade>
<CourseDescription>俗话说学好数理化走遍天下都不怕</CourseDescription>
</Course>
</Courses>
</Student>
2:代码文件编写
映射的实体类
2.1 Student.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace MyXmlSerializer
{
[Serializable]
public class Student
{
//姓名
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
//性别
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}
//年龄
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
//课程集合
private Course[] _courses;
public Course[] Courses
{
get { return _courses; }
set { _courses = value; }
}
}
}
//课程类
[Serializable]
public class Course
{
//科目名称
private string _coursename;
public string CourseName
{
get { return _coursename; }
set { _coursename = value; }
}
//科目分数
private int _grade;
public int Grade
{
get { return _grade; }
set { _grade = value; }
}
//科目描述
private string _coursedescription;
public string CourseDescription
{
get { return _coursedescription; }
set { _coursedescription = value; }
}
}
}
2.2 Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace MyXmlSerializer
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
/// <summary>
/// 把XML文档反序列化为对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeserialize_Click(object sender, EventArgs e)
{
XMLDeserialize();
}
/// <summary>
/// XML文档反序列化
/// </summary>
public void XMLDeserialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Student));
Stream stream = new FileStream("http://www.cnblogs.com/Student.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Student student = xs.Deserialize(stream) as Student;
MessageBox.Show(student.Name);
MessageBox.Show(student.Age.ToString());
MessageBox.Show(student.Courses[0].CourseName);
MessageBox.Show(student.Courses[0].Grade.ToString());
MessageBox.Show(student.Courses[0].CourseDescription);
MessageBox.Show(student.Courses[1].CourseName);
MessageBox.Show(student.Courses[1].Grade.ToString());
MessageBox.Show(student.Courses[1].CourseDescription);
stream.Close();
}
}
}
三:运行效果
当我们点击“把XML文档反序列化为对象”按钮后,依次弹出“小强”,“20”等消息。