Reputation: 2682
Say I have the an xml with a escaped ampersand (&). How do I then read it back such that the result gives me the 'un-escaped' text.
Running the following gives me "&" as the result. How do I get back '&'
void Main()
{
var xml = @"
<a>
&
</a>
";
var doc = new XmlDocument();
doc.LoadXml(xml);
var ele = (XmlElement)doc.FirstChild;
Console.WriteLine (ele.InnerXml);
}
Upvotes: 1
Views: 6591
Reputation: 16144
The following characters are illegal in XML elements:
Illegal EscapedUsed
------------------
" "
' '
< <
> >
& &
To get the unescaped value you can use:
public string UnescapeXMLValue(string xmlValue)
{
if (string.IsNullOrEmpty(s)) return s;
string temp = s;
temp = temp.Replace("'", "'").Replace(""", "\"").Replace(">", ">").Replace("<", "<").Replace("&", "&");
return temp ;
}
To get the escaped value you can use:
public string EscapeXMLValue(string value)
{
if (string.IsNullOrEmpty(s)) return s;
string temp = s;
temp = temp.Replace("'","'").Replace( "\"", """).Replace(">",">").Replace( "<","<").Replace( "&","&");
return temp ;
}
Upvotes: 0
Reputation: 1420
Try using this static method to decode escaped characters: HttpServerUtility.HtmlDecode Method (String) See example here: http://msdn.microsoft.com/ru-ru/library/hwzhtkke.aspx
Upvotes: 0
Reputation: 8207
Console.WriteLine (HttpUtility.UrlDecode(String.Format("{0}",ele.InnerXml)));
Upvotes: 0
Reputation: 29000
you can use CDATA in order to get your data
Characters like "<" and "&" are illegal in XML elements."<" the parser interprets it as the start of a new element. "&" the parser interprets it as the start of an character entity.
Upvotes: 1