将 REST XML 响应反序列化为对象 - 在有效 XML 上失败

本文关键字:XML 有效 失败 REST 响应 反序列化 对象 | 更新日期: 2023-09-27 18:31:01

我尝试反序列化从REST Web服务器获得的xml文档对象。这是我从服务器(有效的XML)得到的确切响应:

<?xml version="1.0" encoding="utf-8"?>
<ads:body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:ads="http://www.ads.org/AnimalDataSchema">
    <ads:header>
        <ads:createdBy>Polaris</ads:createdBy>
        <ads:dateTimeCreated>2016-01-20T10:22:23Z</ads:dateTimeCreated>
    </ads:header>
    <ads:animals count="2">
        <ads:animal>
            <ads:internalIdentifier>1</ads:internalIdentifier>
            <ads:eid>982 000360237659</ads:eid>
            <ads:dob>2012-05-01</ads:dob>
            <ads:breed>Angus</ads:breed>
            <ads:customLifeTimeData>
                <ads:name>VID</ads:name>
                <ads:value>TTDEMO1</ads:value>
            </ads:customLifeTimeData>
        </ads:animal>
        <ads:animal>
            <ads:internalIdentifier>2</ads:internalIdentifier>
            <ads:eid>982 000359951630</ads:eid>
            <ads:dob>2012-05-02</ads:dob>
            <ads:breed>Angus</ads:breed>
            <ads:customLifeTimeData>
                <ads:name>VID</ads:name>
                <ads:value>TTDEMO2</ads:value>
            </ads:customLifeTimeData>
        </ads:animal>
    </ads:animals>
</ads:body>

我用来反序列化的对象类是这样的:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace ***.IOT.Schemas
{
    public class ADI_animal
    {
        [XmlRoot(ElementName = "header", Namespace = "http://www.ads.org/AnimalDataSchema")]
        public class Header
        {
            [XmlElement(ElementName = "createdBy", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string CreatedBy { get; set; }
            [XmlElement(ElementName = "dateTimeCreated", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string DateTimeCreated { get; set; }
        }
        [XmlRoot(ElementName = "customLifeTimeData", Namespace = "http://www.ads.org/AnimalDataSchema")]
        public class CustomLifeTimeData
        {
            [XmlElement(ElementName = "name", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Name { get; set; }
            [XmlElement(ElementName = "value", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Value { get; set; }
        }
        [XmlRoot(ElementName = "animal", Namespace = "http://www.ads.org/AnimalDataSchema")]
        public class Animal
        {
            [XmlElement(ElementName = "internalIdentifier", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string InternalIdentifier { get; set; }
            [XmlElement(ElementName = "eid", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Eid { get; set; }
            [XmlElement(ElementName = "dob", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Dob { get; set; }
            [XmlElement(ElementName = "breed", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Breed { get; set; }
            [XmlElement(ElementName = "customLifeTimeData", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public CustomLifeTimeData CustomLifeTimeData { get; set; }
        }
        [XmlRoot(ElementName = "animals", Namespace = "http://www.ads.org/AnimalDataSchema")]
        public class Animals
        {
            [XmlElement(ElementName = "animal", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public List<Animal> Animal { get; set; }
            [XmlAttribute(AttributeName = "count")]
            public string Count { get; set; }
        }
        [XmlRoot(ElementName = "body", Namespace = "http://www.ads.org/AnimalDataSchema")]
        public class Body
        {
            [XmlElement(ElementName = "header", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public Header Header { get; set; }
            [XmlElement(ElementName = "animals", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public Animals Animals { get; set; }
            [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
            public string Xsi { get; set; }
            [XmlAttribute(AttributeName = "ads", Namespace = "http://www.ads.org/AnimalDataSchema")]
            public string Ads { get; set; }
        }
    }
}

调试输出为:

Exception thrown: 'System.InvalidOperationException' in Microsoft.GeneratedCode
The thread 0xdfc has exited with code 0 (0x0).
The thread 0xb34 has exited with code 0 (0x0).
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.ni.dll
Exception thrown: 'System.InvalidOperationException' in System.Xml.XmlSerializer.dll
exception caught: There is an error in XML document (1, 40).

我运行的代码:

   static public void ProcessResponse(XmlDocument queryResponse)
   {
            String queryRootString;
            ADI_animal adsObject = new ADI_animal();
            using (var stringWriter = new StringWriter())
            using (var xmlTextWriter = XmlWriter.Create(stringWriter))
            {
                XmlDocument queryRoot = queryResponse;
                queryRoot.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                queryRootString = stringWriter.GetStringBuilder().ToString();
            }
            try
            {
                XmlSerializer result = new XmlSerializer(typeof(ADI_animal));
                adsObject = (ADI_animal)result.Deserialize(new StringReader(queryRootString));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("exception caught: " + ex.Message);
            }
}

几乎告诉我XML文档的命名空间有问题。但我就是找不到错误。也许有人可以把我推向正确的方向?

将 REST XML 响应反序列化为对象 - 在有效 XML 上失败

您正在尝试反序列化看起来像ADI_animal.Body的 XML,该序列化程序配置为预期ADI_animal作为顶级对象。

不完全确定您要查找的内容,但希望将该 XML 作为ADI_animal.Body实例应该可以正常工作:

XmlSerializer result = new XmlSerializer(typeof(ADI_animal.Body));
var body = (ADI_animal.Body)result.Deserialize(new StringReader(queryRootString));

调试说明:为了简化反序列化的调查,请先尝试序列化对象,以查看预期的 XML 形状:

XmlSerializer result2 = new XmlSerializer(typeof(ADI_animal));
var w = new StringWriter();
result2.Serialize(w, new ADI_animal());
Console.WriteLine(w.ToString());