C
C#4w ago
Cpt. Reis

(Still) Failing to process an XML API response

hi guys! still quite new to C# and .NET (coming from Java). i am working on handling an XML API response, but continuously fail to achieve this goal. as mentioned, payload is XML format. response headers seem correct (text/xml) and the encoding is set to UTF-8. however, i fail to deserialize the body to an object. tried to add some debug "WriteLine" statements, and saw that the response looked unnatural - what happened is that only the strings between the XML tags were part of the response body, but the tags themselves were lost. and i have no idea what happens there. i've tried a ton of stuff - processing the body as simple string or as stream did not work, the encoding is properly set to Encoding.UTF8... and played around with different solutions, debug logs etc. however, it still doesn't work. interestingly, when using Postman to check actual response from the remote API, i get the very same result (without tags) when selecting "preview" section of the response window. this points me to the direction that i am doing something wrong during reading the response? here's how i deal with the response at the moment:
using var contentStream = response.Content.ReadAsStreamAsync();
using var streamReader = new StreamReader(await contentStream, Encoding.UTF8);
using var xmlTextReader = new XmlTextReader(await streamReader.ReadToEndAsync());
var xmlSerializer = new XmlSerializer(typeof(T));
var deserializedResponse = xmlSerializer.Deserialize(xmlTextReader) as T;
using var contentStream = response.Content.ReadAsStreamAsync();
using var streamReader = new StreamReader(await contentStream, Encoding.UTF8);
using var xmlTextReader = new XmlTextReader(await streamReader.ReadToEndAsync());
var xmlSerializer = new XmlSerializer(typeof(T));
var deserializedResponse = xmlSerializer.Deserialize(xmlTextReader) as T;
(as transfer-encoding header is set to "chunked", I decided to process the response as async stream)
No description
1 Reply
Anton
Anton4w ago
The xml serializer from the standard library is bugged with async afaik see if you have the issues with sync
Want results from more Discord servers?
Add your server