使用html编码或转义字符加载XML或XHTML内容

时间:2010-12-20 18:39:41

标签: c# .net xml c#-4.0 linq-to-xml

我正在为内容管理系统开发一个类。输入内容以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   &#163;</div>

 <div>Registered sign &#174;</div>

 <div>Copyright sign &#169; </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 &#163; &#174; and &#169;
}

但是,此方法会将转义字符转换为其等效字符。如何避免这种情况并保留转义字符。