从字符串中删除转义序列'\'以将其转换为XmlDocument

时间:2021-10-20 00:10:14

I have a web service which returns a struct object, so I get the response as the following XML string. Now I need to load it into XmlDocument object but how do I get rid of the escape sequences in the string. The '\' with every '"' is causing error.

我有一个返回struct对象的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>

3 个解决方案

#1


8  

Use Regex.Unescape method.

使用Regex.Unescape方法。

String unescapedString = Regex.Unescape(textString);

#2


3  

Regex.Unescape doesn't un-escape ". According to the documentation, it does the following:

Regex.Unescape不会逃脱“。根据文档,它执行以下操作:

"..by removing the escape character ("\") from each character escaped by the method. These include the \, *, +, ?, |, {, [, (,), ^, $,., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters."

“..从方法转义的每个字符中删除转义字符(”\“)。包括\,*,+,?,|,{,[,(,),^,$,。,#,另外,Unescape方法取消了右括号(])和右括号(})字符。“

#3


1  

So the webservice is returning the string with actual backslashes in it? If so, I would say there's a problem with that webservice you're using, but you should be able to get around it by doing this:

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

xmlStr = xmlStr.Replace("\\\"", "\"");

#1


8  

Use Regex.Unescape method.

使用Regex.Unescape方法。

String unescapedString = Regex.Unescape(textString);

#2


3  

Regex.Unescape doesn't un-escape ". According to the documentation, it does the following:

Regex.Unescape不会逃脱“。根据文档,它执行以下操作:

"..by removing the escape character ("\") from each character escaped by the method. These include the \, *, +, ?, |, {, [, (,), ^, $,., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters."

“..从方法转义的每个字符中删除转义字符(”\“)。包括\,*,+,?,|,{,[,(,),^,$,。,#,另外,Unescape方法取消了右括号(])和右括号(})字符。“

#3


1  

So the webservice is returning the string with actual backslashes in it? If so, I would say there's a problem with that webservice you're using, but you should be able to get around it by doing this:

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

xmlStr = xmlStr.Replace("\\\"", "\"");