可以透過使用由許多建構函式組成的 XmlSerializer 來控制編碼為 XML 的對象,每當建立序列化程式並且使用的建構函式不採用型別時,就會建立一個臨時組件時間。建立序列化器,允許將物件序列化和反序列化為XML 文件以及XML 文檔,XmlSerializer 的成員為XmlSerializer、XmlSerializer(Type)、XmlSerializer(XmlTypeMapping)、XmlSerializer(Type, String)、XmlSerializer(Type ,類型( )),XmlSerializer(類型,XmlAttributeOverrides),XmlSerializer(類型,XmlRootAttribute),XmlSerializer(類型,XmlAttributeOverrides,類型(),XmlRootAttribute,Yus),XmlSerializer(Apoleptriute(Rootbtri )、XmlAttributeOverrides、Type()、XmlRootAttribute、字串、字串、證據)。在本主題中,我們將學習 C# XmlSerializer。
文法:
XmlSerializer serializer_name = new XmlSerializer(type);
其中serializer_name是XmlSerializer的物件名稱
以下是提到的範例:
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; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫 main 方法,在該方法中建立 XmlSerializer 實例,該實例用於將書籍的詳細資訊編碼為 XML。然後定義一個名為 book 的類,它初始化定義方法 book 的元素和所需屬性,以取得書籍作者的姓名、書名和年份。輸出如上面的快照所示。
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; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫 main 方法,在該方法中建立 XmlSerializer 實例,該實例用於將學生的詳細資料編碼為 XML。然後定義一個名為 Student 的類,該類別初始化元素和所需屬性,這些元素和屬性定義了 Student 方法,以取得學生姓名、科目名稱和主題名稱。輸出如上面的快照所示。
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; } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫 main 方法,在該方法中建立 XmlSerializer 實例,該實例用於將員工的詳細資料編碼為 XML。然後定義一個名為「employee」的類,該類別初始化元素和所需屬性,這些元素和所需屬性定義了方法「employee」以取得僱員的姓名、僱員的名稱和僱員的僱員 ID。輸出如上面的快照所示。
在本教程中,我們透過定義來了解 C# 中 XmlSerializer 的概念、XmlSerializer 的語法,以及透過程式設計範例及其輸出來了解 C# 中 XmlSerializer 的工作原理。
以上是C# XmlSerializer的詳細內容。更多資訊請關注PHP中文網其他相關文章!