>  기사  >  백엔드 개발  >  C# Xml직렬 변환기

C# Xml직렬 변환기

WBOY
WBOY원래의
2024-09-03 15:33:031068검색

XML로 인코딩되는 개체는 수많은 생성자로 구성된 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, 문자열, 문자열, 증거). 이번 주제에서는 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

주어진 도서 세부정보를 XML로 인코딩하는 XmlSerializer를 시연하는 C# 프로그램

코드:

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# Xml직렬 변환기

위 프로그램에는 check라는 클래스가 정의되어 있습니다. 그런 다음 책의 세부 정보를 XML로 인코딩하는 데 사용되는 XmlSerializer 인스턴스가 생성되는 기본 메서드가 호출됩니다. 그런 다음 책 저자의 이름, 책 이름 및 연도를 가져오기 위해 book 메서드를 정의한 요소와 필수 속성을 초기화하는 book이라는 클래스가 정의됩니다. 출력은 위의 스냅샷에 표시됩니다.

예시 #2

주어진 학생 세부 정보를 XML로 인코딩하는 XmlSerializer를 시연하는 C# 프로그램

코드:

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# Xml직렬 변환기

위 프로그램에는 check라는 클래스가 정의되어 있습니다. 그런 다음 학생의 세부 정보를 XML로 인코딩하는 데 사용되는 XmlSerializer 인스턴스가 생성되는 기본 메서드가 호출됩니다. 그런 다음 학생의 이름, 주제의 이름 및 주제의 이름을 사용하기 위해 학생 메소드를 정의한 요소 및 필수 속성을 초기화하는 학생이라는 클래스가 정의됩니다. 출력은 위의 스냅샷에 표시됩니다.

예시 #3

주어진 직원 세부 정보를 XML로 인코딩하는 XmlSerializer를 시연하는 C# 프로그램

코드:

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# Xml직렬 변환기

위 프로그램에는 check라는 클래스가 정의되어 있습니다. 그런 다음 직원의 세부 정보를 XML로 인코딩하는 데 사용되는 XmlSerializer 인스턴스가 생성되는 기본 메서드가 호출됩니다. 그런 다음 직원 이름, 직원 지정 및 직원 ID를 가져오기 위해 직원 메소드를 정의한 요소 및 필수 속성을 초기화하는 직원이라는 클래스가 정의됩니다. 출력은 위의 스냅샷에 표시됩니다.

결론

이 튜토리얼에서는 정의를 통해 C#의 XmlSerializer 개념, XmlSerializer의 구문, 프로그래밍 예제와 출력을 통해 C#의 XmlSerializer 작동 방식을 이해합니다.

위 내용은 C# Xml직렬 변환기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 확장 방법다음 기사:C# 확장 방법