xmlns属性导致C#中的XDocument出错

问题描述 投票:1回答:2

不知何故,<math xmlns='bla'>中的xmlns会导致错误。通过将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", "\&quot;application/x-tex\&quot;"));
            semantics.Add(mrow);
            semantics.Add(annotation);
            mstyle.Add(semantics);
            mathNode.Add(mstyle);
            var s = mathNode.ToString();

            Console.WriteLine(s);
        }
    }

这就是我真正想要的。

  <math xmlns="bla">
  <mstyle>
    <semantics>
      <mrow>
        <SnippetCode>
              testcode1
         </SnippetCode>
      </mrow>
      <annotation encoding="&quot;application/x-tex&quot;" />
    </semantics>
  </mstyle>
</math>
c# xml linq-to-xml
2个回答
0
投票

Ť

  1. 他的问题是:你得到没有命名空间的xml.Descendants(“math”),结果总是为null。
  2. 这行代码是语法错误,因为错误的转义字符(\):

new 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", @"\&quot;application/x-tex\&quot;"));
        semantics.Add(mrow);
        semantics.Add(annotation);
        mstyle.Add(semantics);
        mathNode.Add(mstyle);
        var s = mathNode.ToString();

        Console.WriteLine(s);

看待!


0
投票

xmlns用于命名空间,但在您的情况下,它无处可寻。它用于查找应如何读取XML。它可能是任何东西

xmlns:xsl -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

的xmlns:机器人

的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");

enter image description here

试试下面的代码。它可能会奏效

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", "&quot;application/x-tex&quot;"));
    semantics.Add(mrow);
    semantics.Add(annotation);
    mstyle.Add(semantics);
    mathNode.Add(mstyle);

    var s = mathNode.ToString();
    Console.WriteLine(s);
}
© www.soinside.com 2019 - 2024. All rights reserved.