XML Encode & Decode
Whenever we use XML in your application, we might be required to encode and decode the data which we are passing in the XML since the data may contain some special charecters like XML tags. The charecters '<', '>', '&' and '"' may have to be replaced. I use these methods to encode and decode XML strings.
public string XMLEncode(string Value)
{
return Value.Replace ("&", "&").Replace("'", "'").Replace ("\"", """).Replace ("<", "<").Replace(">", ">");
}
public string XMLDecode(string Value)
{
return Value.Replace ("&", "&").Replace("'", "'").Replace(""", "\"").Replace ("<", "<").Replace (">", ">");
}
4 Comments:
What about XML encoded characters (e.g. "{")?
Hi,
Ive seen that you encoded the quote symbol but while decoding you didn't decode the quote
Thanks Keith for pointing it out. I have updated the post.
Happy Programming.
I have moved the ampersand decoding to the last position to avoid a situation where there is double-decoding as follows:
<Name>JACK && JILL&lt;SAMPLE</Name>
In this example, the < before the SAMPLE is improperly decoded.
Post a Comment
<< Home