• C#温故而知新学习系列之XML编程—XMLSerializer类把XML文档反序列化为对象(八)


      阅读目录

      一:前言

      二:实现步骤

      三:运行效果

      一:前言

      之前我在一家电子商务公司做接口对接工作,同步快递公司给我们网站的订单数据,在这里我先给出快递公司给我的接口文档(见下图),我当时的做法是通过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”等消息。

      

      

      

  • 相关阅读:
    Python实现MapReduce,wordcount实例,MapReduce实现两表的Join
    structure needs cleaning
    Lifecycle of an ASP.NET MVC 5 Application
    ASP.NET Integration with IIS 7
    Execution order of modules in IIS7
    Assembly Binding redirect: How and Why?
    Cannot See Worker Processes Icon in IIS
    What is the main difference between a key, an IV and a nonce?
    核心玩法的三要素
    ruby各种循环输出数组元素
  • 原文地址:https://www.cnblogs.com/menglin2010/p/2307842.html
Copyright © 2020-2023  润新知