0

How can I read below content from a XML file into XDocument. How ever the resulting XDocument should not contain " <!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>
Chat
  • 185
  • 1
  • 5
  • 15

2 Answers2

1

If you want to read it from file:

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

If from string:

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>");
Raider
  • 137
  • 8
1

You can try calling Remove() method on the XDocument.DocumentType property to remove the DOCTYPE information, for exampe :

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

output :

<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>
har07
  • 88,338
  • 12
  • 84
  • 137