我想使用Linq到xml从表达式树创建MathML文档,但我无法弄清楚如何使用MathML xml实体(例如⁡和&InvisibleTimes):当我尝试使用直接创建XElement时
XElement xe = new XElement("mo", "&InvisibleTimes");
它只是逃脱了&符号(这是不好的)。我也试过使用XElement.Parse
XElement xe = new XElement.Parse("<mo>&InvisibleTimes</mo>");
但它失败了System.XmlException:引用未声明的实体'InvisibleTimes'如何声明实体或忽略检查?
正如其他人所指出的那样,没有直接的方法可以做到这一点。
也就是说,您可以尝试使用相应的unicode caracther。根据http://www.w3.org/TR/MathML2/mmlalias.html,对于ApplyFunction,它是02061,尝试新的XElement(“mo”,“\ u02061”)
根据this thread,LINQ to XML不包含实体引用:它没有任何节点类型。它只是在加载文件时扩展它们,然后你就会得到“普通”字符。
我不知道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;
您可能需要对名称进行xml编码,因为“&”是一个特殊字符。
而不是
XElement xe = new XElement("mo", "&InvisibleTimes");
尝试
XElement xe = new XElement("mo", "&InvisibleTimes");
我想你需要一个DTD来定义<mo>&InvisibleTimes; </ mo>。
MathML 2.0提供了XHTML + MathML DTD。
解决方法是使用任何占位符作为;_amp_;
XElement xe = new XElement("mo", ";_amp_;InvisibleTimes)
,并在获取xml字符串时恢复它:output = xe.ToString().Replace(";_amp_;", "&")