How to decode string to XML string in C#
I have a string (from a CDATA element) that contains description of XML. I need to decode this string into a new string that displays the characters correctly using C#
Existing String:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myreport xmlns="http://test.com/rules/client"><admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
String Wanted:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myreport xmlns="http://test.com/rules/client">
<admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
Solution 1:
-
HttpUtility.HtmlDecode
fromSystem.Web
-
WebUtility.HtmlDecode
fromSystem.Net
Solution 2:
You can use System.Net.WebUtility.HtmlDecode instead of HttpUtility.HtmlDecode
Useful if you don't want System.Web reference and prefer System.Net instead.
Solution 3:
As Kirill and msarchet said, you can use HttpUtility.HtmlDecode
from System.Web
. It escapes pretty much anything correctly.
If you don't want to reference System.Web
you might use some trick which supports all XML escaping but not HTML-specific escaping like é
:
public static string XmlDecode(string value) {
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root>" + value + "</root>");
return xmlDoc.InnerText;
}
You could also use a RegEx or simple string.Replace
but it would only support basic XML escaping. Things like А
or é
are examples that would be harder to support.
Solution 4:
HttpUtility.HtmlDecode(xmlString)
will solve this issue