C# XmlSerializer

WBOY
發布: 2024-09-03 15:33:03
原創
1298 人瀏覽過

可以透過使用由許多建構函式組成的 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 的物件時,我們都會使用 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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!