我嘗試反序列化從REST Web服務器獲得的xml文檔對象。這是我從服務器得到確切回覆(有效的XML):將REST 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>
我用deserialise對象類將是這樣:
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文檔在命名空間中存在問題。但我找不到這個錯誤。也許有人能給我一個正確的方向推動?
「看起來像這樣」(並顯示有效的XML)不會有幫助...您需要獲取您嘗試加載的實際字符串(可能會馬上給你答案)。 –
好的。 「看起來像這樣」意思是「這是我得到的XML響應」。對不起,只是我如何制定這個問題。做了一個Debug.WriteLine(queryResponse); ,然後複製並粘貼它(並使用換行符和縮進來格式化它,以便您可以更好地閱讀它)。 –