I am consuming an external API which returns XML. This API returns non-XML value when there is an internal server errors, To get the value I am using ReadToEnd. I am having this interesting error on production....Basically sometimes the returned XML value is not returned as complete and this is causing the xmlDocument.LoadXml to trigger an exception.
try {
using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
// First get value as string and then convert it to XML.
// NOTE: This is implemented in this way as a non-XML value may be returned
responseString = reader.ReadToEnd();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(responseString);
return xmlDocument;
}
} (XmlException xmlEx) {
// Handle Exception as String is not XML....
}
An example of the returned message
<tournamentinfo version="1.0">
<funtables>12</funtables>
<tables>135</tables
<activetables>63</activetables>
<funplayers>0</funplayers>
<realplayers>227</realplayers>
<onlineplayers>731</onlineplayers>
</tourna
Basically, the ending node should be </tournamentinfo>