title: Exception: DTD must be defined before the document root element
body: <p>I am generating an XML document in the following code. At line</p>
  
  <pre><code>xmlDocument.LoadXml(end);
  </code></pre>
  
  <p>throws exception saying: <em>DTD must be defined before the document root element</em></p>
  
  <p>So i found xmlDocument.DocumentType is null and realized Document Type Definition(DTD) must be added to the xmldocument object. so i added </p>
  
  <pre><code> xmlDocument.CreateDocumentType("sample", null, null, null);
  </code></pre>
  
  <p>But still it throws same exception.</p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>            XmlDocument xmlDocument = new XmlDocument()
              {
                  PreserveWhitespace = true
              };
              XmlElement soap = SoapUtil.ToSoap(samlMessage.GetXml(), xmlDocument);
              xmlDocument.AppendChild(soap);
              object[] outerXml = new object[] { xmlDocument.OuterXml };
              SamlTrace.Log(LogLevel.Verbose, "SOAP request to send: {0}", outerXml);
              webRequest.Method = "POST";
              webRequest.ContentType = "text/xml";
              webRequest.Headers.Add("SOAPAction", "http://oasis-open.org/committees/security");
              using (Stream requestStream = webRequest.GetRequestStream())
              {
                  xmlDocument.Save(requestStream);
              }
              using (WebResponse response = webRequest.GetResponse())
              {
                  using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                  {
                      string end = streamReader.ReadToEnd();
                      SamlTrace.Log(LogLevel.Verbose, "Received SOAP response message: {0}", new object[] { end });
  --------&gt;           xmlDocument.LoadXml(end); //exception thrown at this line
                  }
              }
              XmlElement xmlElement1 = SoapUtil.FromSoap(xmlDocument.DocumentElement);
              object[] objArray = new object[] { xmlElement1.OuterXml };
              SamlTrace.Log(LogLevel.Verbose, "Response converted from SOAP message: {0}", objArray);
              xmlElement = xmlElement1;
  </code></pre>
  
  <p>Update 1:</p>
  
  <pre><code> xmlDocument.LoadXml(streamReader.ReadToEnd().ToString());
  </code></pre>
  
  <p>i passed streamReader and converting it to a string.
  Now it throws a new exception.</p>
  
  <p>"<em>Root element is missing.</em>"</p>