Introduction to C# object to XML
The process of storing the state of an object in some form of media like hard drive, stream etc. is called serialization. The objects in C# can be serialized in the format of XML and to be able to convert an object in C# to XML, we will make use of a function called XmlSerializer() function which serializes the given object in C# to XML format. And another function called XmlTextWriter() function to output the serialized XML string and by performing serialization of object in C#. It enables the object to be transferred over the internet, writing to a file becomes easier and complex services can be performed efficiently.
Syntax:
XmlSerializer variable_name = new XmlSerializer();
where variable_name represents the instance of XmlSerializer class.
Steps to convert Object to XML in C# is as follows:
- The process of storing the state of an object in some form of media like hard drive, stream etc. is called serialization and the objects in C# can be serialized in the format of XML.
- To be able to convert an object in C# to XML, we will make use of a function called XmlSerializer() function which serializes the given object in C# to XML format and another function called XmlTextWriter() function to output the serialized XML string.
- Performing serialization of object in C# 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 C# object to XML.
Example #1
C# program to convert the given C# object into XML format and write the contents to a 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:
In the above program, a class called Country is defined within which the two strings name and capital are defined. Then the main method is called within which an instance of XmlSerializer class is created to serialize the C# object Country to XML format. Then an instance of the TextWriter class is created to write the converted XML string to the file in the specified location. The contents of the file in XML format is displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
C# program to convert the given C# object into XML format and write the contents to a 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 above program, a class called Learning is defined within which the two strings organization and topic are defined. Then the main method is called within which an instance of XmlSerializer class is created to serialize the C# object Learning to XML format. Then an instance of the TextWriter class is created to write the converted XML string to the file in the specified location. The contents of the file in XML format is displayed as the output on the screen. The output is shown in the snapshot above.
Example #3
C# program to convert the given C# object into XML format and write the contents to a 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:
In the above program, a class called University is defined within which the two strings name and stream are defined. Then the main method is called within which an instance of XmlSerializer class is created to serialize the C# object University to XML format. Then an instance of the TextWriter class is created to write the converted XML string to the file in the specified location. The contents of the file in XML format is displayed as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of conversion of object to XML in C# using XmlSerializer() function through definition, syntax, and steps to convert an object to XML in C# through programming examples and their outputs.
Recommended Articles
This is a guide to C# object to XML. Here we also discuss the introduction, Steps to convert Object to XML in C# along with different examples. You may also have a look at the following articles to learn more –
6 Online Courses | 18 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.6
View Course
Related Courses