IntroductionIn this tips, I will describe, how to replace the special character in xml. Because few no of char not work in XML or used for xml tag composing. - Ampersand & &
- Left angle bracket < <
- Right angle bracket > >
- Straight quotation mark " "
- Apostrophe ' '
ImplementationIn the .NET introduced the new class to escape special chars easily. The class is SecurityElement. This is class has few no of methods. But here we are going to access Escape method. Method Definition//// Summary:// Replaces invalid XML characters in a string with their valid XML equivalent.//// Parameters:// str:// The string within which to escape invalid characters.//// Returns:// The input string with invalid characters replaced.public static string Escape(string str); CodeSnippetprivate string FormatForXML(object input)
{return SecurityElement.Escape(input.ToString());
}This is straight forward method, just we have to pass whatever string inside the escape method, the .NET do the necessary replacement and return to us.That's all, Just call copy this function and use it your project.Thank you for reading. |