6

In my website I am preparing an array using datagrid items in c#, which works fine, however when I have some text in the grid which has " as value code automatically replaces it with "

i have tried to replace " with " as below, however its failing and replacing " with \"

Array.Value += "|" + dgrFinal.Rows[i].Cells[1].Text.Replace
("amp;", "").Replace(""","\"")

is there any way i can replace " with ".

Can anyone please help me in this.....come on guys you are expert.

9
  • Why do want to replace " with \"?
    – Hary
    Feb 7, 2013 at 11:43
  • Has your GridView AutoGenerateColumns=True? Feb 7, 2013 at 11:43
  • Hi, I want to replace " with " Feb 7, 2013 at 11:45
  • 3
    This sounds like a mismatch between encoded and non-encoded data; perhaps double-encoding. It would be better to find out why it is there, than to replace it... Feb 7, 2013 at 11:46
  • 3
    Are you sure it's failing and replacing with \", rather than you just seeing \" in the debugger, which is how the debugger displays quotes in strings?
    – Rawling
    Feb 7, 2013 at 12:23

3 Answers 3

12

You can use HttpUtility. Here is an example.

using System.Web;

...

string value1 = "<html>";
string value2 = HttpUtility.HtmlDecode(value1);
string value3 = HttpUtility.HtmlEncode(value2);
Debug.WriteLine(value1);
Debug.WriteLine(value2);
Debug.WriteLine(value3);

This is the ouput:

<html>
<html>
&lt;html&gt;

I took the example from here.

6

Have you tried

.Replace("&quot;", @"""")

Edit: outside of a GridView (I'm using LinqPad)

string xxx = "sd&quot;fd";
Console.WriteLine(xxx.Replace("&quot;", @""""));

returns:

sd"fd

If it's still appearing as \" then the battle is with the gridView - I haven't used one for ages but I'd be tempted to try and get the data right before binding to the grid, rather than looping through the rows and cells afterwards.

Edit2: I was going to say it could be the the debugger escaping the quotes but I'm days behind on this one. Good spot @Rawling. At least it's now solved :)

3
  • How is that functionally different than what was already done? I'm pretty sure that they compile to the same string literal. EDIT: (but now I'm doubting myself because I haven't had my morning coffee yet!) Feb 7, 2013 at 11:48
  • @MatíasFidemraizer, @Swanand: With the @ marker for strings, two quotes is how you define a single quote. Feb 7, 2013 at 11:49
  • its converting &quot; to \" Feb 7, 2013 at 11:51
0

Asp.net GridViewRow:

row.Cells[3].Text.Trim().Replace("&quot;", @"""")

Before: &quot;Gasket 22&quot;&quot; rubber ventlation pipes&quot;

After: "Gasket 22"" rubber ventlation pipes"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.