kobe
kobe

Reputation: 15835

Html encode problem for server side strings

I am trying to do html encode on the below string which has quotes , buts it not working

The server returns with quotes for the string

string serverString= **“Test hello,”** // this is returned from database

serverString =HttpUtility.HtmlEncode(serverString);

i am getting this result

�Test helloI,�

but still its not replacing and i am getting some diamond symbols on the asp.net page

Can anybody tell me what am i doing wrong.

Upvotes: 1

Views: 2455

Answers (3)

FishBasketGordo
FishBasketGordo

Reputation: 23122

This is because the server is returning fancy double quotes (that's not the technical name for them) instead of regular double quotes. You could do something like this:

string serverString= "“Test hello,”";
serverString = HttpUtility.HtmlEncode(serverString)
                          // Replaces fancy left double quote with regular one  
                          .Replace("\u2018", "'")
                          // Replaces fancy right double quote with regular one
                          .Replace("\u2019", "'");

Upvotes: 1

StriplingWarrior
StriplingWarrior

Reputation: 156459

The quote characters you're seeing are perfectly legitimate characters from an HTML standpoint, so they don't need to be encoded by HtmlEncode. What you're most likely seeing is an issue with your browser's encoding not supporting those characters. See http://www.htmlbasictutor.ca/character-encoding.htm for more information.

Upvotes: 3

C. Dragon 76
C. Dragon 76

Reputation: 10052

Are you sure it's not a rendering issue? You might try a font like "Arial Unicode MS" to make sure the browser is rendering the characters properly.

You should also verify the string returned from the database is correct.

Lastly, it could help to share how you're writing serverString to your response stream. Some ASP.NET controls expect text and HTML-encode for you while others expect HTML and do not.

Upvotes: 1

Related Questions