string to xml c# code example

Example 1: string to xml c#

string test = "<body><head>test header</head></body>";

XmlDocument xmltest = new XmlDocument();
xmltest.LoadXml(test);

XmlNodeList elemlist = xmltest.GetElementsByTagName("head");

string result = elemlist[0].InnerXml; 

//result -> "test header"

Example 2: como converter String para xml c#

class Program
{
    static void Main(string[] args)
    {
        var stringParaSerializar = "Frase Qualquer";

        var valor = Serializa.SerializaParaString(stringParaSerializar);

        Console.WriteLine(valor);

        Console.ReadKey();
    }
}