我正在使用 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/