2

I am trying to deserialize the following XML response using RestSharp:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
    <ns0:reason>token successfully created</ns0:reason>
    <ns0:success>true</ns0:success>
    <ns0:authDetails>
        <ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
        <ns0:expiresIn>604800</ns0:expiresIn>
        <ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
    </ns0:authDetails>
</ns0:content>

This is a snapshot of my code:

IRestResponse response = client.Execute(request);

RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer();

payload apiresponse = deserial.Deserialize<payload>(response);

And this is the error that I am getting:

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll Additional information: Data at the root level is invalid. Line 1, position 1.

any ideas what I am doing wrong?

2
  • Is there BOM at the beginning? See here. Commented Aug 14, 2015 at 14:29
  • I took the liberty to edit the title of your question since your seem to know how to do it, only you get an error while doing so..
    – helb
    Commented Aug 14, 2015 at 14:36

2 Answers 2

2

Thanks for all the replies.

I did some more investigation and after printing the content of the response to a string, it turned out that RestSharp was actually converting it from XML to JSON. No idea why it was doing that (i certainly wasn't specifying it, perhaps it's a default setting).

So because the response was a JSON then the XML deserializing was obviously throwing an error!

Thanks again.

1

Well, the exception message is pretty clear: Line 1 has invalid syntax:

<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">

The XML should probably look like this instead:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
    <ns0:content>
        <ns0:reason>token successfully created</ns0:reason>
        <ns0:success>true</ns0:success>
        <ns0:authDetails>
            <ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
            <ns0:expiresIn>604800</ns0:expiresIn>
            <ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
        </ns0:authDetails>
    </ns0:content>
</ns0:payload>

If you cannot change how the XML response is generated, you should pre-process the XML using common string-manipulation since it is invalid XML and hence cannot be parsed using standard tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.