title: Loading not-so-well-formed XML into XDocument (multiple DTD)
body: <p>I have got a problem handling data which is almost well-formed XHTML document except for it has multiple DTD declarations in the beginning:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      ...
    &lt;/head&gt;
    &lt;body&gt;
      ...
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I need load this data into XDocument object using only the <em>first</em> DTD and <em>ignoring</em> the rest declarations. It is not possible to completely ignore DTD processing because the document may have unusual characters like <code>&amp;acirc;</code> or <code>&amp;euro;</code> etc.</p>
  
  <p>The text is retrieved from an external source and I have no idea why it comes like this.</p>
  
  <p>Obviously my naive attempt to load this document fails with <code>System.Xml.XmlException : Cannot have multiple DTDs</code>:</p>
  
  <pre><code>        var xmlReaderSettings = new XmlReaderSettings
                                      {
                                          DtdProcessing = DtdProcessing.Parse,
                                          XmlResolver = new XmlPreloadedResolver(),
                                          ConformanceLevel = ConformanceLevel.Document,
                                      };
          using (var xmlReader = XmlReader.Create(stream, xmlReaderSettings))
          {
              return XDocument.Load(xmlReader);
          }
  </code></pre>
  
  <p>What would be the best way to handle this kind of data?</p>
  
  <p>P.S: I forgot to mention, that the data comes from a <code>Stream</code> which may or may not make the string manipulation a little bit more complex</p>