Home  >  Article  >  Backend Development  >  C# object to XML

C# object to XML

王林
王林Original
2024-09-03 15:04:45672browse

Serialization is indeed the process of converting the state of an object into a format that can be stored or transmitted. In C#, objects can be serialized into XML format using the XmlSerializer class. It enables converting C# objects to XML representation, allowing easier transfer over the internet and simplified writing to files.

Syntax:

XmlSerializer variable_name = new XmlSerializer();

where variable_name represents the instance of XmlSerializer class.

Steps to convert an Object to XML in C# are as follows:

  • The process of storing the state of an object in some form of media, like a hard drive, stream, etc., is called serialization, and the objects can be serialized in the format of XML.
  • To be able to convert an object to XML, we will make use of a function called XmlSerializer() function which serializes the given object to XML format, and another function called XmlTextWriter() function to output the serialized XML string.
  • Performing serialization of the object enables the object to be transferred over the internet, writing to a file becomes easier and complex services can be performed efficiently.

Examples

Let us discuss examples of objects to XML.

Example #1

C# program to convert the given object into XML format and write the contents to an XML file stored in the specified location and then display the contents of the file:

Code:

using System.Xml.Serialization;
using System.IO;
//a class called Country is defined within which the two strings are defined
public class Country
{
public string name = "India";
public string capital = "New Delhi";
}
//main method is called
static void Main(string[] args)
{
//an instance of the class country is created
Country c = new Country();
//an instance of the XmlSerializer class is created
XmlSerializer inst = new XmlSerializer(typeof(Country));
//an instance of the TextWriter class is created to write the converted XML string to the file
TextWriter writer = new StreamWriter(@ "C:\Users\admin\Desktop\check.xml");
inst.Serialize(writer, c);
writer.Close();
}

The output of the above program is as shown in the snapshot below:

C# object to XML

Finally, the program displays the file’s contents in XML format as the output on the screen, as depicted in the provided snapshot.

Example #2

C# program to convert the given object into XML format and write the contents to an XML file stored in the specified location and then display the contents of the file:

Code:

using System.Xml.Serialization;
using System.IO;
//a class called Learning is defined within which the two strings are defined
public class Learning
{
public string organization = "EDUCBA";
public string topic = "C#";
}
//main method is called
static void Main(string[] args)
{
//an instance of the class Learning is created
Country c = new Learning();
//an instance of the XmlSerializer class is created
XmlSerializer inst = new XmlSerializer(typeof(Learning));
//an instance of the TextWriter class is created to write the converted XML string to the file
TextWriter writer = new StreamWriter(@ "C:\Users\admin\Desktop\check.xml");
inst.Serialize(writer, c);
writer.Close();
}

The output of the above program is as shown in the snapshot below:

C# object to XML

In the given program, a class named “Learning” defines two strings, “organization” and “topic”. The program then displays the XML-formatted contents of the file as the output on the screen, as shown in the provided snapshot.

Example #3

C# program to convert the given C# object into XML format and write the contents to an XML file stored in the specified location and then display the contents of the file:

Code:

using System.Xml.Serialization;
using System.IO;
//a class called University is defined within which the two strings are defined
public class University
{
public string name = "VTU";
public string stream = "BE";
}
//main method is called
static void Main(string[] args)
{
//an instance of the class University is created
Country c = new University();
//an instance of the XmlSerializer class is created
XmlSerializer inst = new XmlSerializer(typeof(University));
//an instance of the TextWriter class is created to write the converted XML string to the file
TextWriter writer = new StreamWriter(@ "C:\Users\admin\Desktop\check.xml");
inst.Serialize(writer, c);
writer.Close();
}

The output of the above program is as shown in the snapshot below:

C# object to XML

The program defines a class called University, which defines two strings: name and stream. Then it calls the main method, which creates an instance of the XmlSerializer class to serialize the University object to XML format. It then creates an instance of the TextWriter class to write the converted XML string to a file at the specified location. Finally, it displays the contents of the file, which are in XML format, as the output on the screen.

Conclusion – C# object to XML

In this article, we have learned the concept of conversion of an object to XML using XmlSerializer() function through definition, syntax, and steps to convert an object to XML through programming examples and their outputs.

The above is the detailed content of C# object to XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Composition C#Next article:Composition C#