Need to URL encode each character only if its a Unicode character or range of Unicode characters but leave the non-Unicode characters alone if they have asci-ii representations using linq expressions from this link.
Also I'm trying to get the system to detect if a unicode character exists first before attempting to urlencode it.
DataTable dt = new DataTable();
dt.TableName = "testcase1";
dt.Columns.Add("LABEL");
dt.Columns.Add("VALUE");
for(int i=0; i<10; i++)
{
DataRow row = dt.NewRow();
row["LABEL"] = "test ®±¶ me ™.";
row["VALUE"] = i;
dt.Rows.Add(row);
}
// now use linq expression to URL Encode LABEL field
var rowsToUpdate = dt.AsEnumerable().Where(a => a.Field<string>("LABEL").Length > 0);
foreach (var row in rowsToUpdate)
{
row.SetField("LABEL", Uri.EscapeUriString(row[0].ToString()));
}
foreach (DataRow row in dt.Rows)
{
Console.WriteLine(row["LABEL"].ToString());
Console.WriteLine(row["VALUE"].ToString());
}
The main reason I want to do this is that if I url encode the entire string it makes the spaces look like plus symbols and formatting is horrendous when I use it in an asp.net label or text box.
Also in ASP.NET I'm using the HttpContext.Current.Server.UrlEncode function to encode the value. In the console app example above I'm using the Uri.EscapeUriString function.