首页 > 后端开发 > C#.Net教程 > C# XmlSerializer

C# XmlSerializer

WBOY
发布: 2024-09-03 15:33:03
原创
1356 人浏览过

可以通过使用由许多构造函数组成的 XmlSerializer 来控制编码为 XML 的对象,每当创建序列化程序并且使用的构造函数不采用类型时,就会创建一个临时程序集时间。创建序列化器,允许将对象序列化和反序列化为 XML 文档以及 XML 文档,XmlSerializer 的成员为 XmlSerializer、XmlSerializer(Type)、XmlSerializer(XmlTypeMapping)、XmlSerializer(Type, String)、XmlSerializer(Type ,类型()),XmlSerializer(类型,XmlAttributeOverrides),XmlSerializer(类型,XmlRootAttribute),XmlSerializer(类型,XmlAttributeOverrides,类型(),XmlRootAttribute,字符串),XmlSerializer(类型,XmlAttributeOverrides,类型(),XmlRootAttribute,字符串,字符串)、XmlAttributeOverrides、Type()、XmlRootAttribute、字符串、字符串、证据)。在本主题中,我们将学习 C# XmlSerializer。

语法:

XmlSerializer serializer_name = new XmlSerializer(type);
登录后复制

其中serializer_name是XmlSerializer的对象名称

C# 中 XmlSerializer 的工作

  • 每当需要控制编码为 XML 的对象时,我们都会使用 C# 中的 XmlSerializer。
  • XmlSerializer 由许多构造函数组成。
  • 每当创建序列化器并且使用的构造函数不采用类型时,每次都会创建一个临时程序集,创建序列化器,它允许将对象序列化和反序列化到 XML 文档中以及从文档中XML。
  • XmlSerializer 的成员有 XmlSerializer、XmlSerializer( Type )、XmlSerializer( XmlTypeMapping )、XmlSerializer( Type, String )、XmlSerializer( Type, Type() )、XmlSerializer( Type, XmlAttributeOverrides )、XmlSerializer( Type, XmlRootAttribute ), XmlSerializer( Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String ), XmlSerializer( Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String, String ), XmlAttributeOverrides, Type(), XmlRootAttribute, String, String, Evidence )。

C# XmlSerializer 示例

以下是提到的示例:

示例#1

C# 程序演示 XmlSerializer 将给定的书籍详细信息编码为 XML。

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//a class called check is defined
public class check
{
//main method is called within which the instance of XmlSerializer is created which is used to encode the details of the book into XML
public static void Main()
{
XmlSerializer ser_name = new XmlSerializer(typeof(Book));
Book bookdetails = new Book("Shobha Shivakumar", "Meaning of life", 2020);
ser_name.Serialize(Console.Out, bookdetails);
Console.ReadLine();
}
}
//a class called book is defined which initializes the elements and required attributes which defines the method book to take the name of the author of the book, name of the book and the year
public class Book
{
[XmlElementAttribute("AuthorName")]
public string authorname;
[XmlAttributeAttribute("BookName")]
public string bookname;
[XmlAttributeAttribute("YearofPublishing")]
public int year;
public Book()
{
}
public Book(string authorname, string bookname, int year)
{
this.authorname = authorname;
this.bookname = bookname;
this.year = year;
}
}
登录后复制

输出:

C# XmlSerializer

在上面的程序中,定义了一个名为check的类。然后调用 main 方法,在该方法中创建 XmlSerializer 实例,该实例用于将书籍的详细信息编码为 XML。然后定义一个名为 book 的类,它初始化定义方法 book 的元素和所需属性,以获取书籍作者的姓名、书名和年份。输出如上面的快照所示。

示例#2

C# 程序演示 XmlSerializer 将给定的学生详细信息编码为 XML。

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//a class called check is defined
public class check
{
//main method is called within which the instance of XmlSerializer is created which is used to encode the details of the book into XML
public static void Main()
{
XmlSerializer ser_name = new XmlSerializer(typeof(Student));
Student studentdetails = new Student("Shobha Shivakumar", "C Sharp", "XML");
ser_name.Serialize(Console.Out, studentdetails);
Console.ReadLine();
}
}
//a class called student is defined which initializes the elements and required attributes which defines the method student to take the name of the student, name of the student and name of the topic
public class Student
{
[XmlElementAttribute("StudentName")]
public string studentname;
[XmlAttributeAttribute("SubjectName")]
public string subjectname;
[XmlAttributeAttribute("TopicName")]
public string topicname;
public Student()
{
}
public Student(string studentname, string subjectname, string topicname)
{
this.studentname = studentname;
this.subjectname = subjectname;
this.topicname = topicname;
}
}
登录后复制

输出:

C# XmlSerializer

在上面的程序中,定义了一个名为check的类。然后调用 main 方法,在该方法中创建 XmlSerializer 实例,该实例用于将学生的详细信息编码为 XML。然后定义一个名为 Student 的类,该类初始化元素和所需属性,这些元素和属性定义了 Student 方法,以获取学生姓名、科目名称和主题名称。输出如上面的快照所示。

示例 #3

C# 程序演示 XmlSerializer 将给定员工详细信息编码为 XML。

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//a class called check is defined
public class check
{
//main method is called within which the instance of XmlSerializer is created which is used to encode the details of the book into XML
public static void Main()
{
XmlSerializer ser_name = new XmlSerializer(typeof(Employee));
Employee employeedetails = new Employee("Shobha Shivakumar", "Engineer", 123);
ser_name.Serialize(Console.Out, employeedetails);
Console.ReadLine();
}
}
//a class called employee is defined which initializes the elements and required attributes which define the method employee to take the name of the employee, the designation of the employee and the employee ID of the employee
public class Employee
{
[XmlElementAttribute("EmployeeName")]
public string Employeename;
[XmlAttributeAttribute("Designation")]
public string Designation;
[XmlAttributeAttribute("EmployeeID")]
public int EmployeeID;
public Employee()
{
}
public Employee(string Employeename, string Designation, int EmployeeID)
{
this.Employeename = Employeename;
this.Designation = Designation;
this.EmployeeID = EmployeeID;
}
}
登录后复制

输出:

C# XmlSerializer

在上面的程序中,定义了一个名为check的类。然后调用 main 方法,在该方法中创建 XmlSerializer 实例,该实例用于将员工的详细信息编码为 XML。然后定义一个名为“employee”的类,该类初始化元素和所需属性,这些元素和所需属性定义了方法“employee”以获取雇员的姓名、雇员的名称和雇员的雇员 ID。输出如上面的快照所示。

结论

在本教程中,我们通过定义了解 C# 中 XmlSerializer 的概念、XmlSerializer 的语法,以及通过编程示例及其输出了解 C# 中 XmlSerializer 的工作原理。

以上是C# XmlSerializer的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板