Decodes the XML escape sequences into normal string - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Decodes the XML escape sequences into normal string

Demo Code


using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using System;/*  ww  w .j  a v a 2 s .  c o  m*/

public class Main{
        /// <summary>
        /// Decodes the XML escape sequences into normal string
        /// </summary>
        /// <param name="str">The string to decode.</param>
        /// <returns></returns>
        public static string DecodeXMLString(string str)
        {
            if (str.IndexOf('&') >= 0)
            {
                return str.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'").Replace("&amp;", "&");
                // Make sure that &amp; is the final replace so that sequences such as &amp;gt; do not get corrupted
            }

            return str;
        }
}

Related Tutorials