We can use the SecurityElement’s Escape method of the System.Security namespace to escape all the special characters in an XML file. Code snippet below:
string escapedstring = System.Security.SecurityElement.Escape(inputXMLString);
…inputXMLString ?contains the XML with special characters.