1

I have the following xml and I'm trying to deserialize it. I have used the RestSharp library but with this particular response I'm getting an empty list of resources. Do you know if the library has problem with self-closing elements in XML?

<?xml version="1.0" encoding="utf-8"?>
<resourceList xmlns="..." xmlns:xsi="..." xsi:schemaLocation="..." location="...">
    <resourceURL location="../../../campaign/12" metaData1="12" metaData2="ACD Systems" metaData3="CPS" metaData4="Active" metaData5="Public" metaData6="No" metaData7="2005-10-28" metaData8="2020-12-31"/>
    <resourceURL location="../../../campaign/10607" metaData1="10607" metaData2="Aiseesoft" metaData3="CPS" metaData4="Active" metaData5="Public" metaData6="No" metaData7="2013-07-17" metaData8="2033-12-31"/>
    <resourceURL location="../../../campaign/567" metaData1="567" metaData2="AnchorFree Hotspot Shield Elite" metaData3="CPS" metaData4="Active" metaData5="Select" metaData6="No" metaData7="2011-10-03" metaData8="2031-12-31"/>
    <resourceURL location="../../../campaign/384" metaData1="384" metaData2="Avast" metaData3="CPS" metaData4="Active" metaData5="Select" metaData6="No" metaData7="2008-06-30" metaData8="2025-01-31"/>
</resourceList>

C# Classes:

[DeserializeAs(Name = "resourceList")]
public class ResourceList
{
    [DeserializeAs(Name = "resourceURL")]
    public List<ResourceUrl> ResourceURL { get; set; }
}

public class ResourceUrl
{
    public string Location { get; set; }

    public string MetaData1 { get; set; }

    public string MetaData2 { get; set; }
}

Last my call:

var response = _restClient.Execute<ResourceList>(request);

1 Answer 1

1

No, RestSharp doesn't have any problems with self-closing elements in XML.

As you are using their default XML deserialiser (RestSharp.Deserializers.XmlDeserializer), as can be inferred by the use of the DeserializeAs attribute, you can see in the library's source code that they use internally the XML parser from System.Xml.

So your XML sample should be deserialised without problems. But for that I recommend a simpler and faster solution:

using System.Collections.Generic;
using RestSharp.Deserializers;

public class ResourceList : List<ResourceUrl> {}

[DeserializeAs(Name = "resourceURL")]
public class ResourceUrl {
    [DeserializeAs(Name = "location")]
    public string Location { get; set; }
    [DeserializeAs(Name = "metaData1")]
    public string MetaData1 { get; set; }
    [DeserializeAs(Name = "metaData2")]
    public string MetaData2 { get; set; }
}

//(...)

request.RootElement = null;
request.XmlNamespace = null;
var response = _restClient.Execute<ResourceList>(request);

However, this solution doesn't work with the current v105.2.3 (the one still on NuGet).

You have to use the version of the class committed at the GitHub repository. Just get the code for the class, change the top namespace statement to your own, save to it disk and add it to you project. Then, just set the RestSharp client to use the new (better) version of the deserialiser:

_restClient.AddHandler("application/xml",new XmlDeserializer());
Sign up to request clarification or add additional context in comments.

1 Comment

Finally this answer helped me. Interesting, that RestSharp web page itself does not explain deserialization easily...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.