Search code examples
c#htmlentitieshtml-encode

Is there a method in C# that converts just the characters with a special meaning in HTML and XML to entities?


My programme generates an XML file with data from a database that contains special HTML/XML characters, so those need to be converted to entities.

I tried to use System.Web.HttpUtility.HtmlEncode() for that, which works but it does much more than I want.

Certain (but not all) non-ASCII characters such as the middle dot and superscript numbers are also converted even though they have no special meaning in HTML and XML.

I get for instance the string a·ba·ĵur·o instead of simply a·ba·ĵur·o.

As you see the circumflexed "j" is left as it is, but the dots are converted. This makes some data in the file difficult to read for humans;

I only want <, >, &, ' and " converted, nothing more.

Is there a method in C# that does that, or do I need to write my own?


Solution

  • Use SecurityElement.Escape("your HTML")