以某种方式 xmlns 导致错误。通过将<math xmlns='bla'>
更改为xmlns
不会发生错误。
是什么原因,有解决方案吗?
xmlnss
那是我真正想要的。
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
public class Program{
public static void Main(){
string mathMLResult = @"<math xmlns='bla'>
<SnippetCode>
testcode1
</SnippetCode>
</math>";
XDocument xml = XDocument.Parse(mathMLResult);
XElement mathNode = xml.Descendants("math").FirstOrDefault();
// error occurres in this line
List<XNode> childNodes = mathNode.Nodes().ToList();
XElement mrow = new XElement("mrow");
mrow.Add(childNodes);
mathNode.RemoveNodes();
XElement mstyle = new XElement("mstyle");
XElement semantics = new XElement("semantics");
XElement annotation = new XElement("annotation",
new XAttribute("encoding", "\"application/x-tex\""));
semantics.Add(mrow);
semantics.Add(annotation);
mstyle.Add(semantics);
mathNode.Add(mstyle);
var s = mathNode.ToString();
Console.WriteLine(s);
}
}
答案 0 :(得分:0)
T
新的XAttribute(“ encoding”,“ \” application / x-tex \“”)));
我已经解决了。请看一下:
string mathMLResult = @"<math xmlns='bla'>
<SnippetCode>
testcode1
</SnippetCode>
</math>";
XDocument xml = XDocument.Parse(mathMLResult);
XElement mathNode = xml.Descendants().FirstOrDefault(x => x.Name.LocalName == "math");
// error occurres in this line
List<XNode> childNodes = mathNode.Nodes().ToList();
XElement mrow = new XElement("mrow");
mrow.Add(childNodes);
mathNode.RemoveNodes();
XElement mstyle = new XElement("mstyle");
XElement semantics = new XElement("semantics");
XElement annotation = new XElement("annotation",
new XAttribute("encoding", @"\"application/x-tex\""));
semantics.Add(mrow);
semantics.Add(annotation);
mstyle.Add(semantics);
mathNode.Add(mstyle);
var s = mathNode.ToString();
Console.WriteLine(s);
问候!
答案 1 :(得分:0)
xlmns用于名称空间,但是在您的情况下,它根本找不到。它用于查找应如何读取XML。可能什么都可以
xmlns:xsl -
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xmlns:android
xmlns:xhtml
在这里检查
https://www.w3.org/TR/REC-xml-names/
https://www.w3schools.com/xml/xml_namespaces.asp
如果您仍然想要这样做,请尝试以下代码,但是xml应该是正确的格式
XDocument xDoc = XDocument.Parse(xml);
XNamespace bla = "bla";
我尝试了以下代码,但没有给出错误
string mathMLResult = @"<math xmlns='bla'>
<SnippetCode>
testcode1
</SnippetCode>
</math>";
XDocument xmld = XDocument.Parse(mathMLResult);
XNamespace bla = "bla";
var mathItem = xmld.Element(bla + "math");
var SnippetCodeItem = mathItem.Element(bla + "SnippetCode");
尝试以下代码。可能有效
static XElement stripNS(XElement root)
{
return new XElement(
root.Name.LocalName,
root.HasElements ?
root.Elements().Select(el => stripNS(el)) :
(object)root.Value
);
}
static void Main(string[] args)
{
string mathMLResult = @"<math xmlns='bla'>
<SnippetCode>
testcode1
</SnippetCode>
</math>";
XDocument xmld = XDocument.Parse(mathMLResult);
XNamespace bla = "bla";
var mathNode = xmld.Element(bla + "math");
mathNode = stripNS(mathNode);
List<XNode> childNodes = mathNode.Nodes().ToList();
XElement mrow = new XElement("mrow");
mrow.Add(childNodes);
mathNode.RemoveNodes();
XElement mstyle = new XElement("mstyle");
XElement semantics = new XElement("semantics");
XElement annotation = new XElement("annotation",
new XAttribute("encoding", ""application/x-tex""));
semantics.Add(mrow);
semantics.Add(annotation);
mstyle.Add(semantics);
mathNode.Add(mstyle);
var s = mathNode.ToString();
Console.WriteLine(s);
}