1

我想使用 Linq to xml 从表达式树创建 MathML 文档,但我不知道如何使用 MathML xml 实体(例如 ⁡ 和 &InvisibleTimes):当我尝试使用

XElement xe = new XElement("mo", "&InvisibleTimes");

它只是逃脱了&符号(这不好)。我也尝试使用 XElement.Parse

XElement xe = new XElement.Parse("<mo>&InvisibleTimes</mo>");

但它失败了 System.XmlException: Reference to undeclared entity 'InvisibleTimes' 如何声明实体或忽略检查?

4

6 回答 6

2

根据这个线程,LINQ to XML 不包括实体引用:它没有任何节点类型。它只是在加载文件时扩展它们,然后你就得到了“普通”字符。

于 2009-04-01T20:10:42.170 回答
1

正如其他人指出的那样,没有直接的方法可以做到这一点。

也就是说,您可以尝试使用相应的 unicode 字符。根据http://www.w3.org/TR/MathML2/mmlalias.html,对于 ApplyFunction 它是 02061,尝试 new XElement("mo", "\u02061")

于 2009-04-01T20:55:17.103 回答
0

我不知道XDocument,但你可以这样做XmlDocument

XmlDocument doc = new XmlDocument();
var entity = doc.CreateEntityReference("InvisibleTimes");
XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("xml"));
var el = root.AppendChild(doc.CreateElement("mo")).AppendChild(entity);
string s = doc.OuterXml;
于 2009-04-01T20:00:37.420 回答
0

您可能需要对名称进行 xml 编码,因为“&”是一个特殊字符。

所以而不是

XElement xe = new XElement("mo", "&InvisibleTimes");

尝试

XElement xe = new XElement("mo", "&amp;InvisibleTimes");
于 2009-04-01T20:05:00.390 回答
0

我认为您需要一个 DTD 来定义 <mo>⁢</mo>。

MathML 2.0 提供了 XHTML + MathML DTD。

于 2009-04-01T20:18:13.930 回答
0

一种解决方法是对 & 使用任何占位符,例如;_amp_; XElement xe = new XElement("mo", ";_amp_;InvisibleTimes) 在获取 xml 字符串时将其恢复: output = xe.ToString().Replace(";_amp_;", "&")

于 2019-03-27T23:21:40.903 回答