RestSharp:如何对 XmlDeserializer 这个响应?
RestSharp: how to XmlDeserializer this response?
注意在 repositories-item 列表之前有额外的 repositories 和数据标签。我使用 NexusRepo
class 来建模 repositories-item
,但出现此错误:
Exception caught: System.ApplicationException: Error retrieving response. Check inner details for more info. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at RestSharp.Deserializers.XmlDeserializer.HandleListDerivative(XElement root, String propName, Type type)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
--- End of inner exception stack trace ---
public List<NexusRepo> GetAllRepos()
{
RestRequest request = new RestRequest();
//RestRequest request = new RestRequest("/repositories", Method.GET);
request.Resource = "/repositories";
request.RootElement = "NexusRepo";
return this.restSharpAPI.Execute<List<NexusRepo>>(this.client, request);
}
public class NexusRepo
{
public string name { get; set; }
public string format { get; set; }
public string resourceURI { get; set; }
public override string ToString()
{
string s = "name=" + name + "format=" + format + "resourceURI=" + resourceURI;
return s;
}
}
问题是 NexusRepo
class 名称与 repositories-item
节点名称不同。您可以添加 [DeserializeAs]
属性来解决它:
[DeserializeAs(Name = "repositories-item")]
public class NexusRepo
或者您可以重命名 class 或节点以满足以下条件之一:
When searching data for a matching element, the default XML
deserializer will look for elements and attributes in the following
order:
- Element with exact name match
- Element with name matching lower-cased property name
- Element with name matching camel-cased property name
- Element with underscores and dashes removed from name
- Attribute with exact name match
- Attribute with name matching lower-cased property name
- Attribute with name matching camel-cased property name
- Attribute with underscores and dashes removed from name
如official docs中所述。
在我之后有效:
- 添加了属性
- 添加使用 RestSharp.Deserializers;
注释掉下面的 RootElement 行。
//request.RootElement = "NexusRepo";
注意在 repositories-item 列表之前有额外的 repositories 和数据标签。我使用 NexusRepo
class 来建模 repositories-item
,但出现此错误:
Exception caught: System.ApplicationException: Error retrieving response. Check inner details for more info. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at RestSharp.Deserializers.XmlDeserializer.HandleListDerivative(XElement root, String propName, Type type)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
--- End of inner exception stack trace ---
public List<NexusRepo> GetAllRepos()
{
RestRequest request = new RestRequest();
//RestRequest request = new RestRequest("/repositories", Method.GET);
request.Resource = "/repositories";
request.RootElement = "NexusRepo";
return this.restSharpAPI.Execute<List<NexusRepo>>(this.client, request);
}
public class NexusRepo
{
public string name { get; set; }
public string format { get; set; }
public string resourceURI { get; set; }
public override string ToString()
{
string s = "name=" + name + "format=" + format + "resourceURI=" + resourceURI;
return s;
}
}
问题是 NexusRepo
class 名称与 repositories-item
节点名称不同。您可以添加 [DeserializeAs]
属性来解决它:
[DeserializeAs(Name = "repositories-item")]
public class NexusRepo
或者您可以重命名 class 或节点以满足以下条件之一:
When searching data for a matching element, the default XML deserializer will look for elements and attributes in the following order:
- Element with exact name match
- Element with name matching lower-cased property name
- Element with name matching camel-cased property name
- Element with underscores and dashes removed from name
- Attribute with exact name match
- Attribute with name matching lower-cased property name
- Attribute with name matching camel-cased property name
- Attribute with underscores and dashes removed from name
如official docs中所述。
在我之后有效:
- 添加了属性
- 添加使用 RestSharp.Deserializers;
注释掉下面的 RootElement 行。
//request.RootElement = "NexusRepo";