繁体   English   中英

XML反序列化尝试产生空对象

[英]XML deserialization attempt yields empty object

我正在尝试在控制台应用程序中反序列化一些XML。 我只关心一些数据,因此我创建了一个仅包含所需字段的类。 该程序运行,但是我反序列化的PetFinderPetRecord具有所有空成员(所有字符串为null,所有int为0)。

这是XML:

<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
<version>0.1</version>
<timestamp>2014-10-23T04:12:37Z</timestamp>
<status>
<code>100</code>
<message/>
</status>
</header>
<pet>
<id>29165893</id>
<shelterId>VA72</shelterId>
<shelterPetId/>
<name>Buckeye and Hawkeye</name>
<animal>Cat</animal>
<breeds>
<breed>Domestic Short Hair-black</breed>
</breeds>
<mix>no</mix>
<age>Baby</age>
<sex>M</sex>
<size>M</size>
<options>
<option>hasShots</option>
<option>altered</option>
<option>housetrained</option>
</options>
<description>
<![CDATA[
Buckeye and Hawkeye are about 6 months old as of 5/6/14. Buckeye and his brother, Hawkeye, are very bonded and hope to find a home together. They are very playful and love to have their chin scratched. They get along very well with other cats and are curious and lovable. They will make a wonderful addition to your family. Please email jleach1234@aol.com for more information.
]]>
</description>
<lastUpdate>2014-05-07T21:30:42Z</lastUpdate>
<status>A</status>
<media>
<photos>
<photo id="1" size="pnt">
http://photos.petfinder.com/photos/pets/29165893/1/?bust=1399498241&width=60&-pnt.jpg
</photo>
<photo id="1" size="fpm">
http://photos.petfinder.com/photos/pets/29165893/1/?bust=1399498241&width=95&-fpm.jpg
</photo>
<photo id="1" size="x">
http://photos.petfinder.com/photos/pets/29165893/1/?bust=1399498241&width=500&-x.jpg
</photo>
<photo id="1" size="pn">
http://photos.petfinder.com/photos/pets/29165893/1/?bust=1399498241&width=300&-pn.jpg
</photo>
<photo id="1" size="t">
http://photos.petfinder.com/photos/pets/29165893/1/?bust=1399498241&width=50&-t.jpg
</photo>
<photo id="2" size="pnt">
http://photos.petfinder.com/photos/pets/29165893/2/?bust=1399498241&width=60&-pnt.jpg
</photo>
<photo id="2" size="fpm">
http://photos.petfinder.com/photos/pets/29165893/2/?bust=1399498241&width=95&-fpm.jpg
</photo>
<photo id="2" size="x">
http://photos.petfinder.com/photos/pets/29165893/2/?bust=1399498241&width=500&-x.jpg
</photo>
<photo id="2" size="pn">
http://photos.petfinder.com/photos/pets/29165893/2/?bust=1399498241&width=300&-pn.jpg
</photo>
<photo id="2" size="t">
http://photos.petfinder.com/photos/pets/29165893/2/?bust=1399498241&width=50&-t.jpg
</photo>
</photos>
</media>
<contact>
<address1>PO Box 7040</address1>
<address2/>
<city>Fairfax Station</city>
<state>VA</state>
<zip>22039</zip>
<phone>(703) 940-9183</phone>
<fax/>
<email>Jleach1234@aol.com</email>
</contact>
</pet>
</petfinder>

这是控制台代码:

public class Class1
{
    static void Main(string[] args)
    {
        string URL = "http://api.petfinder.com/pet.getRandom";
        string urlParameters = "?key=myprivatekey&id=ID123&status=A&format=xml&output=full";

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/xml"));

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;
        if (response.IsSuccessStatusCode)
        {
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "petfinder";
            xRoot.IsNullable = true;
            XmlSerializer serializer = new XmlSerializer(typeof(PetFinderPetRecord), xRoot);

            PetFinderPetRecord record = null;

            using (Stream stream = response.Content.ReadAsStreamAsync().Result)
            {
                record = (PetFinderPetRecord)serializer.Deserialize(stream); // <-- has empty members
            }
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
    }
}

这是我要反序列化的类:

[Serializable()]
public class PetFinderPetRecord
{
    [XmlElement("id")]
    public int id { get; set; }

    [XmlElement("shelterId")]
    public int shelterId { get; set; }

    [XmlElement("shelterPetId")]
    public int shelterPetId { get; set; }

    [XmlElement("name")]
    public string name { get; set; }

    [XmlElement("animal")]
    public string animal { get; set; }

    [XmlElement("mix")]
    public string mix { get; set; }

    [XmlElement("age")]
    public string age { get; set; }

    [XmlElement("sex")]
    public string sex { get; set; }

    [XmlElement("size")]
    public string size { get; set; }

    [XmlElement("description")]
    public string description { get; set; }

    [XmlElement("lastupdate")]
    public DateTime lastupdate { get; set; }

    [XmlElement("status")]
    public string status;

    public PetFinderPetRecord()
    {

    }
}

如果有人可以告诉我我所缺少或正在做错的事情,我将不胜感激。 先感谢您。

不知道这是否是唯一的问题,但是在以下代码行上:

            HttpResponseMessage response = client.GetAsync(urlParameters).Result;

您应该等待电话:

            HttpResponseMessage response = await client.GetAsync(urlParameters).Result;

否则,您的if语句可以在服务器响应之前执行

注意:为此,您还需要将async关键字添加到方法声明中

static void async Main(string[] args)

我希望这有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM