5

我有一个返回结构对象的 Web 服务,所以我得到的响应是以下 XML 字符串。现在我需要将它加载到 XmlDocument 对象中,但是如何摆脱字符串中的转义序列。每个 '"' 的 '\' 都会导致错误。

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Quote xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\">
<price>19656</price>
</Quote>
4

4 回答 4

15

使用Regex.Unescape方法。

String unescapedString = Regex.Unescape(textString);
于 2014-12-03T03:12:48.220 回答
5

Regex.Unescape不会取消转义"。根据文档,它执行以下操作:

..通过从该方法转义的每个字符中删除转义字符(“”)。其中包括 、*、+、?、|、{、[、(,)、^、$、.、# 和空白字符。此外,Unescape 方法对右括号 (]) 和右大括号 (}) 字符进行转义。

于 2015-04-29T21:34:46.963 回答
2

那么网络服务正在返回带有实际反斜杠的字符串吗?如果是这样,我会说你正在使用的那个 web 服务有问题,但你应该能够通过这样做来解决它:

xmlStr = xmlStr.Replace("\\\"", "\"");
于 2013-02-11T10:29:51.437 回答
0

您可以尝试在逐字字符串中转义

xmlStr= xmlStr.Contains("\\") ? xmlStr.Replace("\\", @"\"") : xmlStr;
于 2019-08-19T05:49:50.297 回答