我对C#相当新,所以我在这里问。
我正在使用一个返回长字符串XML值的Web服务。由于这是一个字符串,所有属性都有转义的双引号。
string xmlSample = "<root><item att1="value" att2="value2" /></root>"
这是我的问题。我想要做一个简单的字符串替换。如果我在PHP中工作,我只需运行strip_slashes()。
然而,我在C#中,我无法理解它。我无法写出替换双引号(“)的表达式,因为它将终止字符串。如果我转义它,那么结果会不正确。我做错了什么?
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: 在原始字符串中包含引用,您需要将其加倍。
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>"