use*_*673 35 .net c# xml xml-serialization
我有一个字符串(来自CDATA元素),其中包含XML的描述.我需要将此字符串解码为一个新的字符串,使用C#正确显示字符
现有字符串:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myreport xmlns="http://test.com/rules/client"><admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
Run Code Online (Sandbox Code Playgroud)
字符串通缉:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myreport xmlns="http://test.com/rules/client">
<admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
Run Code Online (Sandbox Code Playgroud)
mat*_*res 39
您可以使用System.Net.WebUtility.HtmlDecode而不是HttpUtility.HtmlDecode
如果您不想要System.Web参考并且更喜欢System.Net,则非常有用.
作为基里尔和msarchet说,你可以使用HttpUtility.HtmlDecode从System.Web.它正确地逃脱了任何事情.
如果您不想引用System.Web,可能会使用一些支持所有XML转义的技巧,但不支持特定于HTML的转义é:
public static string XmlDecode(string value) {
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root>" + value + "</root>");
return xmlDoc.InnerText;
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用RegEx或简单,string.Replace但它只支持基本的XML转义.像是А或者é是更难以支持的例子.
| 归档时间: |
|
| 查看次数: |
62252 次 |
| 最近记录: |