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

发布于 2024-10-08 11:54:45 字数 1376 浏览 2 评论 0原文

我正在开发一个内容管理系统的课程。输入内容以 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;
}

但是,此方法将转义字符转换为其等效字符。我怎样才能避免这种情况并保留转义的字符。

I'm developing a class for a content management system. The input content is supplied in XHTML format. And it can contain valid escaped characters like £ See the example below.

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

My objective is to write a method that loads this to an XML .Net object do some processing and save to database. I want to maintain the escaped characters as they are. And here is my method:

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 ©
}

This method however converts the escaped characters to their character equivalents. How can I avoid this and keep the escaped characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文