Remove the escape sequence '\' from string to convert it to XmlDocument

て烟熏妆下的殇ゞ 提交于 2019-12-22 06:51:04

问题


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.

<?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>

回答1:


Use Regex.Unescape method.

String unescapedString = Regex.Unescape(textString);



回答2:


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

"..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."




回答3:


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:

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



回答4:


You can try escaping in verbatim strings

xmlStr= xmlStr.Contains("\\") ? xmlStr.Replace("\\", @"\"") : xmlStr;


来源:https://stackoverflow.com/questions/14810215/remove-the-escape-sequence-from-string-to-convert-it-to-xmldocument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!