Rumah > Artikel > pembangunan bahagian belakang > C# XmlSerializer
Objek yang sedang dikodkan ke dalam XML boleh dikawal dengan menggunakan XmlSerializer yang terdiri daripada banyak pembina dan apabila serializer dicipta dan pembina yang digunakan adalah sesuatu yang tidak mengambil jenis, maka pemasangan sementara dibuat setiap masa. Serializer dicipta yang membolehkan pensirilan dan penyahserikatan objek ke dalam dokumen XML dan daripada dokumen XML dan ahli XmlSerialiizer ialah XmlSerializer, XmlSerializer(Jenis), XmlSerializer(XmlTypeMapping), XmlSerializer(Jenis, String), XmlSerializer(Jenis). , Type()), XmlSerializer(Type, XmlAttributeOverrides), XmlSerializer(Type, XmlRootAttribute), XmlSerializer(Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String), XmlSerializer(Type, XmlAttributeOverrides, String), XmlSerializer(Jenis, XmlAttributeOverrides, String) ), XmlAttributeOverrides, Type(), XmlRootAttribute, String, String, Evidence). Dalam topik ini, kita akan belajar tentang C# XmlSerializer.
Sintaks:
XmlSerializer serializer_name = new XmlSerializer(type);
di mana serializer_name ialah nama objek XmlSerializer
Contoh #1
Kod:
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; } }
Output:
Dalam program di atas, kelas yang dipanggil semak ditentukan. Kemudian kaedah utama dipanggil di mana contoh XmlSerializer dicipta yang digunakan untuk mengekod butiran buku ke dalam XML. Kemudian kelas dipanggil buku ditakrifkan yang memulakan elemen dan atribut yang diperlukan yang menentukan buku kaedah untuk mengambil nama pengarang buku, nama buku dan tahun. Output ditunjukkan dalam syot kilat di atas.
Contoh #2
Kod:
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; } }
Output:
Dalam program di atas, kelas yang dipanggil semak ditentukan. Kemudian kaedah utama dipanggil di mana contoh XmlSerializer dicipta yang digunakan untuk mengekod butiran pelajar ke dalam XML. Kemudian kelas dipanggil pelajar ditakrifkan yang memulakan elemen dan atribut yang diperlukan yang menentukan kaedah pelajar untuk mengambil nama pelajar, nama subjek dan nama topik. Output ditunjukkan dalam syot kilat di atas.
Contoh #3
Kod:
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; } }
Output:
Dalam program di atas, kelas yang dipanggil semak ditentukan. Kemudian kaedah utama dipanggil di mana contoh XmlSerializer dicipta yang digunakan untuk mengekod butiran pekerja ke dalam XML. Kemudian kelas yang dipanggil pekerja ditakrifkan yang memulakan elemen dan atribut yang diperlukan yang menentukan kaedah pekerja untuk mengambil nama pekerja, penetapan pekerja dan ID pekerja pekerja. Output ditunjukkan dalam syot kilat di atas.
Kesimpulan
Atas ialah kandungan terperinci C# XmlSerializer. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!