C# Linq到Xml和自定义Xml实体
我想使用Linq to xml从表达式树创建MathML文档,但我不知道如何使用MathML xml实体(例如&ApplyFunction;和&InvisibleTimes): 当我尝试使用C# Linq到Xml和自定义Xml实体,c#,xml,linq,mathml,C#,Xml,Linq,Mathml,我想使用Linq to xml从表达式树创建MathML文档,但我不知道如何使用MathML xml实体(例如&ApplyFunction;和&InvisibleTimes): 当我尝试使用 XElement xe = new XElement("mo", "&InvisibleTimes"); 它只是逃脱了符号(这是不好的)。 我还尝试使用XElement.Parse XElement xe = new XElement.Parse("<mo>&Invisible
XElement xe = new XElement("mo", "&InvisibleTimes");
它只是逃脱了符号(这是不好的)。
我还尝试使用XElement.Parse
XElement xe = new XElement.Parse("<mo>&InvisibleTimes</mo>");
XElement xe=new-XElement.Parse(“&InvisibleTimes”);
但由于System.XmlException失败:引用未声明的实体“InvisibleTimes”
如何声明实体或忽略检查?我不知道
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");
试一试
根据,LINQtoXML不包括实体引用:它没有任何节点类型。它只是在加载文件时展开它们,然后您就得到了“普通”字符。我认为您需要一个DTD来定义&InvisibleTimes
MathML2.0提供了一个XHTML+MathMLDTD。正如其他人所指出的,没有直接的方法可以做到这一点
也就是说,您可以尝试使用相应的unicode caracther。根据,对于ApplyFunction,它是02061,请尝试使用new-XElement(“mo”,“u02061”)一种解决方法是使用任何占位符作为&例如
_amp!代码>
XElement xe=new-XElement(“mo”,“不可见时间”)
并在获取xml字符串时将其还原:
output=xe.ToString().Replace(“;amp_;;”,“&”)
(我编辑以显示格式)看起来实际上让事情变得更糟:&;invisibleMesthis不起作用:“XElement(“mo”),“\u02061”)。我不知道为什么这是公认的答案。。。
XElement xe = new XElement("mo", "&InvisibleTimes");