我对c#很新,所以这就是为什么我在这里问这个问题。C#String.Replace双引号和文字
我正在使用一个Web服务,该服务返回一长串XML值。因为这是一个字符串,所有的属性都逃过了双引号
string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"
这是我的问题。我想做一个简单的string.replace。如果我在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:?要包括你需要加倍原始字符串的报价。
这是重要的信息,现在想这种做法......没有运气或者 有一些事情在这里的双引号。的概念大家建议是坚实的,但这里的问题是处理双引号,它看起来像我需要做一些addit离子研究来解决这个问题。如果有人提出了一些问题,请发表一个答案。
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>"
绝对不要在这里使用的正则表达式,只需使用替换功能 – Domenic 2008-11-12 22:42:15
看到最后的答案 - 你已经完成你想要的东西,这就是你看着它被搞乱您的方式 – 2008-11-12 23:22:22