Sometimes when creating XML documents with user specific
data, string values of the XML nodes have to be encoded to maintain the
structure of the XML nodes. Following table shows invalid XML characters and
their escaped equivalent.
Invalid XML Character
|
Escaped equivalent
|
“<”
|
"<"
|
“>”
|
">"
|
“\””
|
"""
|
“\’”
|
"'"
|
“&”
|
“&"
|
There is a build-in method in .NET to achieve this called SecurityElement.Escape
under the system. Security namespace. This
method accept string parameter and returns a string with invalid characters
replaced.
e.g :-
string xml = "\"node\"&";
string encodedXml = System.Security.SecurityElement.Escape(xml);
//RESULT: <node>"node"&<node>
No comments:
Post a Comment