Кому читать XML-файл нижеследующего содержимого в Xdocument

Как я могу прочитать содержимое ниже из файла XML в XDocument. Как бы то ни было, полученный XDocument не должен содержать " <!DOCTYPE math:math PUBLIC "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN" "math.dtd">"

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE math:math PUBLIC "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN" "math.dtd">
    <math:math xmlns:math="http://www.w3.org/1998/Math/MathML">
        <math:semantics>
            <math:msup>
                <math:mrow>
                    <math:mo math:stretchy="false">(</math:mo>
                    <math:mrow>
                        <math:mi>a</math:mi>
                        <math:mo math:stretchy="false">+</math:mo>
                        <math:mi>b</math:mi>
                    </math:mrow>
                    <math:mo math:stretchy="false">)</math:mo>
                </math:mrow>
                <math:mn>2</math:mn>
            </math:msup>
            <math:annotation math:encoding="StarMath 5.0">(a+b)^2</math:annotation>
        </math:semantics>
    </math:math>

2 ответа

Если вы хотите прочитать это из файла:

XDocument doc = XDocument.Load("yourfilepath");

Если из строки:

XDocument doc = XDocument.Parse("<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE math:math PUBLIC "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN" "math.dtd">
    <math:math xmlns:math="http://www.w3.org/1998/Math/MathML">
        <math:semantics>
            <math:msup>
                <math:mrow>
                    <math:mo math:stretchy="false">(</math:mo>
                    <math:mrow>
                        <math:mi>a</math:mi>
                        <math:mo math:stretchy="false">+</math:mo>
                        <math:mi>b</math:mi>
                    </math:mrow>
                    <math:mo math:stretchy="false">)</math:mo>
                </math:mrow>
                <math:mn>2</math:mn>
            </math:msup>
            <math:annotation math:encoding="StarMath 5.0">(a+b)^2</math:annotation>
        </math:semantics>
    </math:math>");

Вы можете попробовать позвонить Remove() метод на XDocument.DocumentType свойство для удаления информации DOCTYPE, например:

var xml = @"<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE math:math PUBLIC '-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN' 'math.dtd'>
<math:math xmlns:math='http://www.w3.org/1998/Math/MathML'>
    <math:semantics>
        <math:msup>
            <math:mrow>
                <math:mo math:stretchy='false'>(</math:mo>
                <math:mrow>
                    <math:mi>a</math:mi>
                    <math:mo math:stretchy='false'>+</math:mo>
                    <math:mi>b</math:mi>
                </math:mrow>
                <math:mo math:stretchy='false'>)</math:mo>
            </math:mrow>
            <math:mn>2</math:mn>
        </math:msup>
        <math:annotation math:encoding='StarMath 5.0'>(a+b)^2</math:annotation>
    </math:semantics>
</math:math>";
var doc = XDocument.Parse(xml);
doc.DocumentType.Remove();
Console.WriteLine(doc.ToString());

Dotnetfiddle Demo

выход:

<math:math xmlns:math="http://www.w3.org/1998/Math/MathML">
  <math:semantics>
    <math:msup>
      <math:mrow>
        <math:mo math:stretchy="false">(</math:mo>
        <math:mrow>
          <math:mi>a</math:mi>
          <math:mo math:stretchy="false">+</math:mo>
          <math:mi>b</math:mi>
        </math:mrow>
        <math:mo math:stretchy="false">)</math:mo>
      </math:mrow>
      <math:mn>2</math:mn>
    </math:msup>
    <math:annotation math:encoding="StarMath 5.0">(a+b)^2</math:annotation>
  </math:semantics>
</math:math>
Другие вопросы по тегам