反序列化XML Rest WebApi调用? [英] Deserialize XML Rest WebApi Call?

查看:60
本文介绍了反序列化XML Rest WebApi调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注一些在线示例,由于某种原因,我无法成功反序列化以下XML文档:

I have been following some online examples and for some reason I cannot successfully deserialize the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <postcode>BD1 1LT</postcode>
    <geo>
        <lat>53.793063240118215</lat>
        <lng>-1.7540318423563699</lng>
        <easting>416300.0</easting>
        <northing>433000.0</northing>
        <geohash>http://geohash.org/gcwf00dz21g1</geohash>
    </geo>
    <administrative>
        <council>
            <title>Bradford</title>
            <uri>http://statistics.data.gov.uk/id/statistical-geography/E08000032</uri>
            <code>E08000032</code>
        </council>
        <ward>
            <title>City</title>
            <uri>http://statistics.data.gov.uk/id/statistical-geography/E05001347</uri>
            <code>E05001347</code>
        </ward>
        <constituency>
            <title>Bradford West</title>
            <uri>http://statistics.data.gov.uk/id/statistical-geography/E14000589</uri>
            <code>E14000589</code>
        </constituency>
    </administrative>
</result>

我尝试了各种组合:

[DataContract(Name = "result", Namespace = "")]
public class Result
{
    [DataMember(Name = "postcode")]
    public string Postcode { get; set; }
    [DataMember(Name = "geo")]
    public Geo Geo { get; set; } 
}

[DataContract(Name = "geo")]
public class Geo
{
    [DataMember(Name = "lat")]
    public string Lat { get; set; } 
}

但是我能得到的只是邮政编码,没有别的。 上面 是我所拥有的 工作自动取款机

But all I can get is the post code and nothing else. Above is what I have working atm.

下面是我写的以下示例一起显示:

Below is what I have put together following some examples:

    [Test]
    public void TestRestCall()
    {
        var url = "http://uk-postcodes.com/postcode/";
        var urlParameters = "bd11lt.xml";
        var client = new HttpClient();
        client.BaseAddress = new Uri(url);

        // Add an Accept header for JSON format.
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

        // List data response.
        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
        if (response.IsSuccessStatusCode)
        {
            // Parse the response body. Blocking!
            var dataObjects = response.Content.ReadAsAsync<Result>().Result;
            //Console.WriteLine("{0}", dataObjects.Geo.);
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        } 
    }

UPDATE:

UPDATE:

Geo属性为null,这是我所能做到的。

The Geo property is null and that's as far as I can get with this.

推荐答案

我看到了两个问题。一个与顺序有关,另一个与名称空间有关。

There are two problems that I see. One is with the ordering and another is with namespace.

您需要在所有属性和对象上指定顺序,但还需要在所有属性和对象上指定名称空间

You need to specify the order on all of your properties and objects but you also need to specify the namespace on all of them.

    [DataContract(Name = "result", Namespace = "")]
    public class Result
    {
        [DataMember(Name = "postcode", Order = 1)]
        public string Postcode { get; set; }

        [DataMember(Name = "geo", Order = 2)]
        public Geo Geo { get; set; }
    }

就像在GEO对象上一样,您必须同时指定两者才能使映射起作用

Likewise on your GEO object you have to specify both for the mapping to work on the HttpClient.

    [DataContract(Name = "geo", Namespace = "")]
    public class Geo
    {
        [DataMember(Name = "lat", Order = 1)]
        public string Lat { get; set; }
    }

希望有帮助。

这篇关于反序列化XML Rest WebApi调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆