Home > Article > Backend Development > C# object to XML
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:
Let us discuss examples of objects to XML.
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:
Finally, the program displays the file’s contents in XML format as the output on the screen, as depicted in the provided snapshot.
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:
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.
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:
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.
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!