kaiser31
kaiser31

Reputation: 1

escape ("<") special chracters in c#

I have a problem with results are empty because of special characters like < are not searching in my query results so what I have done is this code (I have already tested this with Regex and WebUtility.HtmlDecode("&lt;") it always returns an empty result.)

private IEnumerable < string > GetRowValues(DataRow dr) {
  foreach(DataColumn col in DataResult.Columns)
    yield return replaceSpecialChar(dr[col].ToString());
}

private string replaceSpecialChar(string text) {

  if (text.Contains("<")) {

    text = text.Replace("<", "&lt");

    System.Diagnostics.Debug.WriteLine(text);
  }

  return text;
}

Upvotes: 0

Views: 63

Answers (1)

fubo
fubo

Reputation: 45947

You're mixing up encode and decode

string result = WebUtility.HtmlEncode("<"); //&lt;

Upvotes: 3

Related Questions