c# - LINQ to XML 会忽略 DTD 中的包含吗?

标签 c# xml linq-to-xml dtd mathml

我正在使用 MathML DTD 来使用 System.Xml.Linq 解析 MathML。 虽然普通的 MathML 内容可以很好地识别,但包含在 DTD 中的 MMLEXTRA被忽略,我得到错误。这是我正在使用的代码:

  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()。任何帮助表示赞赏!

最佳答案

来自 here

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.

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"));

所有信息均来自同一个 source

关于c# - LINQ to XML 会忽略 DTD 中的包含吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391274/

相关文章:

c# - 从 xml 文件接收错误代码值

c# - 使用 LinqToXml 插入 XElement 以围绕另一个 XElement

c# - Telerik 网格自定义列构建/格式化

xml - 在 XML 中包装任意 XML

xml - 具有多个谓词的 Xpath 表达式

python - BeautifulSoup 使用可迭代而不是字符串?

c# - 选择一些特定的 xml 元素到匿名对象列表

c# - 使用 Func 作为 LinqToEntities 的参数

c# - WPF:布局的最佳实践是什么

c# - 循环迭代器偏移量