1

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.

Community
  • 1
  • 1
RetroCoder
  • 2,597
  • 10
  • 52
  • 81
  • 1
    looks like you'd end up with a per-character solution. if ascii is the set you would like to exclude from encoding, go by the numeric value of each char - when its between 32 and 126 print it, otherwise encode it? or use a const string of all characters not to encode. – Cee McSharpface Feb 05 '16 at 23:19
  • Just realized I was trying to use an item template in a asp.net control and an attribute was the main issue: "AllowCustomText="true"... gahhhhh – RetroCoder Feb 09 '16 at 02:49

0 Answers0