LINQ to XML是否会忽略DTD中的包含内容? [英] Does LINQ to XML ignore includes from a DTD?

查看:89
本文介绍了LINQ to XML是否会忽略DTD中的包含内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MathML DTD通过 System.Xml.Linq来解析MathML。虽然普通的MathML东西可以被很好地识别,但 MMLEXTRA 包含在DTD中会被忽略,并且会出现错误。这是我正在使用的代码:

I'm using the MathML DTD for parsing MathML using System.Xml.Linq. While the ordinary MathML stuff gets recognized fine, the MMLEXTRA include in the DTD gets ignored and I get errors. Here's the code I'm using:

  if (!string.IsNullOrWhiteSpace(mathML))
  {
    try
    {
      const string preamble =
          "<!DOCTYPE mml:math PUBLIC \"-//W3C//DTD MathML 2.0//EN\"\n" +
           "\"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd\" [\n" +
           "<!ENTITY % MATHML.prefixed \"INCLUDE\">\n" +
           "<!ENTITY % MATHML.prefix \"mml\"> \n" +
         "]>";
      var parsed = Parser.Parse(preamble + Environment.NewLine + mathML);
      textEditor.Text = printed;
      lblStatus.Caption = "MathML successfully translated.";
    } 
    catch (Exception e)
    {
      lblStatus.Caption = "Cannot translate text. " + e.Message;
    }
  }

解析器只执行 XDocument .Load()

推荐答案

来自此处


DTD中的实体是本质上是不安全的。包含DTD的
恶意XML文档有可能导致解析器使用
的所有内存和CPU时间,从而导致拒绝服务攻击。
因此,在LINQ to XML中,DTD处理默认情况下处于关闭状态。
您不应接受来自不受信任来源的DTD。

Entities in DTDs are inherently not secure. It is possible for a malicious XML document that contains a DTD to cause the parser to use all memory and CPU time, causing a denial of service attack. Therefore, in LINQ to XML, DTD processing is turned off by default. You should not accept DTDs from untrusted sources.

但是,要启用它,您应该使用XDocumentType类

However, to enable it you should use XDocumentType class.

一些可能的解决方案:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;

XmlReader reader = XmlReader.Create(Server.MapPath("filename"), settings);

XDocument doc = XDocument.Load(reader);

或者也许:

 XDocument xDocument = new XDocument(new XDocumentType("Books",null,"Books.dtd", null),new XElement("Book"));

所有信息均来自同一源文件

这篇关于LINQ to XML是否会忽略DTD中的包含内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆