我正在为内容管理系统开发一个类。输入内容以XHTML格式提供。它可以包含有效的转义字符,例如£
请参阅下面的示例。
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head xmlns="">
<meta name="Attr_DocumentTitle" content="Hello World Books" />
</head>
<body>
<div>British Pound £</div>
<div>Registered sign ®</div>
<div>Copyright sign © </div>
</body>
</html>
我的目标是编写一个方法,将其加载到XML .Net对象进行一些处理并保存到数据库。我想保持转义字符不变。这是我的方法:
public static XmlDocument LoadXmlFromString(string xhtmlContent)
{
byte[] xhtmlByte = Encoding.ASCII.GetBytes(xhtmlContent);
MemoryStream mStream = new MemoryStream(xhtmlByte);
XmlReaderSettings settings = new XmlReaderSettings();
//Upon loading XML, prevent DTD download, which would be blocked by our
//firewall and generate "503 Server Unavailable" error.
settings.XmlResolver = null;
settings.ProhibitDtd = false;
XmlReader reader = XmlReader.Create(mStream, settings);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xhtmlContent);
return xmlDoc; //Value of xmlDoc.InnerXml contains £ ® © in place
// of £ ® and ©
}
但是,此方法会将转义字符转换为其等效字符。如何避免这种情况并保留转义字符。
答案 0 :(得分:2)