Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String.Replace double quotes and Literals

Tags:

c#

xml

I'm fairly new to c# so that's why I'm asking this here.

I am consuming a web service that returns a long string of XML values. Because this is a string all the attributes have escaped double quotes

string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"

Here is my problem. I want to do a simple string.replace. If I was working in PHP I'd just run strip_slashes().

However, I'm in C# and I can't for the life of me figure it out. I can't write out my expression to replace the double quotes (") because it terminates the string. If I escape it then it has incorrect results. What am I doing wrong?

    string search = "\\\"";
    string replace = "\"";
    Regex rgx = new Regex(search);
    string strip = rgx.Replace(xmlSample, replace);

    //Actual Result  <root><item att1=value att2=value2 /></root>
    //Desired Result <root><item att1="value" att2="value2" /></root>

MizardX: To include a quote in a raw string you need to double it.

That's important information, trying that approach now...No luck there either There is something going on here with the double quotes. The concepts you all are suggesting are solid, BUT the issue here is dealing with the double quotes and it looks like I'll need to do some additional research to solve this problem. If anyone comes up with something please post an answer.

string newC = xmlSample.Replace("\\\"", "\"");
//Result <root><item att=\"value\" att2=\"value2\" /></root> 

string newC = xmlSample.Replace("\"", "'");
//Result newC   "<root><item att='value' att2='value2' /></root>"
like image 856
discorax Avatar asked Nov 12 '08 22:11

discorax


3 Answers

the following statement in C#

string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"

will actually store the value

<root><item att1="value" att2="value2" /></root>

whereas

string xmlSample = @"<root><item att1=\""value\"" att2=\""value2\"" /></root>";

have the value of

<root><item att1=\"value\" att2=\"value2\" /></root>

for the second case, you need to replace the slash () by empty string as follow

string test = xmlSample.Replace(@"\", string.Empty);

the result will be

<root><item att1="value" att2="value2" /></root>

P.S.

  1. slash (\) is default escape character in C#
  2. to ignore slashes, use @ at the beginning of string
  3. if @ is used, the escape character is double quote (")
like image 63
ala Avatar answered Nov 18 '22 10:11

ala


There's no reason to use a Regular expression at all... that's a lot heavier than what you need.

string xmlSample = "blah blah blah";

xmlSample = xmlSample.Replace("\\\", "\"");
like image 28
Timothy Khouri Avatar answered Nov 18 '22 10:11

Timothy Khouri


Both the string and the regex uses \ for escaping. The regex will see the character \ followed by ", and think it's a literal escape. Try this:

Regex rgx = new Regex("\\\\\"");
string strip = rgx.Replace(xmlSample, "\"");

You could also use raw strings (also known as veratim strings) in C#. They are prefixed with @, and all back-slashes are treated as normal characters. To include a quote in a raw string you need to double it.

Regex rgx = new Regex(@"\""")
string strip = rgx.Replace(xmlSample, @"""");

like image 2
Markus Jarderot Avatar answered Nov 18 '22 10:11

Markus Jarderot