Mohanapriya R Mohanapriya R
Updated date Jun 23, 2024
In this article, we will learn how to convert XML elements into strings using C#.

Converting XML Elements to Strings in C#

We will see how to convert XML Elements to Strings using C#.

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlElement = xmlDoc.CreateElement("fruit");
        xmlElement.InnerText = "I like Orange";

        string elementAsString = xmlElement.OuterXml;
        
        Console.WriteLine("XML Element as String:");
        Console.WriteLine(elementAsString);
        Console.ReadKey();
    }
}

In the above program, we have created an XML element named "fruit" with the content "I like Orange". Then, we converted this XML element into a string using the OuterXml property. 

Output:

XML Element as String:
<fruit>I like Orange</fruit>

Comments (0)

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