How to Work with XML in C#


Like HTML, XML is a markup language designed to carry data. A few pointers on XML are:
• XML contain only user-defined tags
• XML does not process
• XML does not contain any keyword.
The advantage of using XML to exchange data is that it is easily readable by both human and computer. In C#, XML is present in the class System.Xml.

Step 1: Implementation

XML in C# is used to transfer and receive data. To document the XML in C#, the functionality is present in the name space system.Xml.Linq.

To create a C# XML file, XDocument is utilized. srcTree is the name of the XML file that Contains a comment, “This is a comment” followed by a root element and four child element. The elements are added using the keyword ‘XElement’. The following code explains the creation of the XML file.

Code – XML documentation

Step 2 – Output

It is simple to print a C# XML file. To read an XML document Tree, Console.WriteLine(Tree); is coded to get the output. Since XML has a pre-defined format, the printline statement prints the XML file in complete format. The output is as follows:

Output