XML attributes deserialization with RestSharp

倖福魔咒の 提交于 2019-12-25 09:39:25

问题


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:


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());


来源:https://stackoverflow.com/questions/36894380/xml-attributes-deserialization-with-restsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!