Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

c# - HtmlEncode with HTML entity name, is it possible?

I am using the following method to HtmlEncode some text that it's in Spanish, like this:

string word = "configuración";
string encodedWord = System.Net.WebUtility.HtmlEncode(word);

The output is the expected:

configuración

But! the ó text represents the HTML entity number for a latin small letter "o" with acute.

However, I want to know if there is a way - using a built-in function which I don't know, library, etc - to show the HTML entity name of the HTML entity number and also support other characters (like a generic solution).

What I've tried so far is to check for a HTML entities table (there were many when Googling but I used this one: http://www.ascii.cl/htmlcodes.htm) then created a custom method for replacing the needed string from the word by doing some mapping.

So, if the word contains ó then the matching text will be replaced to it's HTML entity name which is oacute; but it is really painful since there are plenty of cases/scenarios.

Finally, the desired output will be:

configuración
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

HtmlEncode(word); does only encode ISO 8859-1 (Latin-1). Which means your input needs to be encoded in ISO 8859-1. The ó is not in the iso standard, you can try to use the AntiXss encoder:

Microsoft.Security.Application.AntiXss.HtmlEncode("ó"); 

or Microsoft.Security.Application.Encoder.HtmlEncode("ó");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...