Mohanapriya R Mohanapriya R
Updated date May 20, 2024
In this article, we will learn how to convert XML to strings in C#.

XML to String Conversion in C#:

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        // Load the XML document
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(@"<bookstore>
                          <book>
                            <title>Introduction to C#</title>
                            <author>John Doe</author>
                            <price>29.99</price>
                          </book>
                        </bookstore>");

        // Convert XML document to string
        string xmlString = xmlDoc.OuterXml;

        // Display the converted string
        Console.WriteLine("Converted XML to String:");
        Console.WriteLine(xmlString);
    }
}

In this program, we create an instance of XmlDocument and load the XML content into it. The OuterXml property is then used to obtain the XML representation as a string. Finally, we print the converted string to the console.

Output:

Converted XML to String:
<bookstore>
  <book>
    <title>Introduction to C#</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
</bookstore>

Comments (0)

There are no comments. Be the first to comment!!!