3

Is there any way to do HTML encoding in C# when characters like apostrophes encoded like ' instead of ' as in case of HttpUtility.HtmlEncode method?

I have the DB that contains a string like this:

str'str

and I need to select it via SQL query when the user type "str'str" in the TextBox. So I want to encode it to the same string first and select it via LIKE operator then.

Thanks in advance.

10
  • How do you mean "is there any other way"? A dozen, but anything specific? Commented Mar 31, 2015 at 11:15
  • Why not use the method you mention yourself? Commented Mar 31, 2015 at 11:16
  • @Patrick Hofman @Alejandro I need to encode characters like apostrophes as ' instead of ' Commented Mar 31, 2015 at 11:17
  • 1
    Why? They are the same. Commented Mar 31, 2015 at 11:18
  • 1
    @PatrickHofman He stores strings containing "'" as a string, for example "Mike's". Now when from his UI he wants to search "Mike's", he wants to convert the search term to "Mike's" instead of "Mike&39;s", so he can perform a string search. One way could be to store "Mike's" in the database instead of "Mike's"... Commented Mar 31, 2015 at 11:31

3 Answers 3

4

apos is an xml entity, not really HTML. You can use the AntiXSS library to encode as xml to get your apostrophes, but I don't know what other data you need to encode and if it will meet your needs but worth a try:

System.Web.Security.AntiXss.AntiXssEncoder.XmlEncode("Mike's")
Sign up to request clarification or add additional context in comments.

Comments

1
        string teststr = " > < & ";
        string zoo = HttpUtility.HtmlEncode(teststr);

zoo == " & gt; & lt; & amp; " (Minus the spaces between the ampersand and alpha characters)

Comments

1
string str = "str'str";

Option 1:

System.Security.SecurityElement.Escape(str);

Option 2:

string encoded = str.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");

for more info take a look at xml encoding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.