0

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>

kidra.pazzo
  • 195
  • 8
  • 1
    Surely this is an issue for the creators of the API you're consuming to deal with? – ProgrammingLlama Apr 04 '18 at 13:28
  • If authors of this api return responses with status code 200 in case of errors (as your code suggests) - I won't rely on them to do everything right (such as always return complete xml responses). – Evk Apr 04 '18 at 13:34
  • How big is the actual XML document that you're expecting? – Paul Michaels Apr 04 '18 at 13:35
  • From my machine, the whole XML value is being returned without any truncated value. The XML that is being returned is around 1MB – kidra.pazzo Apr 04 '18 at 13:39
  • That might be your issue - have a look at this question: https://stackoverflow.com/questions/38334720/c-sharp-is-there-a-limit-to-the-size-of-an-httpwebrequest-stream – Paul Michaels Apr 04 '18 at 16:33

1 Answers1

0

Basically, this issue arose because a call to the provider's API was being performed while the provider was updating his RSS feed.

kidra.pazzo
  • 195
  • 8