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)