次のデータを返す Web サービスがあります。
<?xml version=""1.0"" encoding=""UTF-8""?>
<RESPONSE>
<KEY>12345</KEY>
<PROPERTY>
<PROPERTY_ADDRESS>
<STREET_NUM>25</STREET_NUM>
<STREET_ADDRESS>ELM ST</STREET_ADDRESS>
<STREET_PREFIX/>
<STREET_NAME>ELM</STREET_NAME>
<STREET_TYPE>ST</STREET_TYPE>
<STREET_SUFFIX/>
</PROPERTY_ADDRESS>
</PROPERTY>
</RESPONSE>
私は一致するクラス構造を持っています:
[DataContract(Name="RESPONSE", Namespace="")]
public class Response
{
[DataMember(Name="KEY")]
public string Key { get; set; }
[DataMember(Name = "PROPERTY")]
public Property Property { get; set; }
}
[DataContract(Name="PROPERTY", Namespace="")]
public class Property
{
[DataMember(Name="PROPERTY_ADDRESS")]
public PropertyAddress Address { get; set; }
}
[DataContract(Name="PROPERTY_ADDRESS", Namespace="")]
public class PropertyAddress
{
[DataMember(Name="STREET_NUM")]
public string StreetNumber { get; set; }
[DataMember(Name = "STREET_ADDRESS")]
public string StreetAddress { get; set; }
[DataMember(Name = "STREET_PREFIX")]
public string StreetPrefix { get; set; }
[DataMember(Name = "STREET_NAME")]
public string StreetName { get; set; }
[DataMember(Name = "STREET_TYPE")]
public string StreetType { get; set; }
[DataMember(Name = "STREET_SUFFIX")]
public string StreetSuffix { get; set; }
}
私の逆シリアル化コードは次のようになります。
[Test]
public void TestMapping()
{
var serializer = new DataContractSerializer(typeof(Response));
Response response = null;
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(XmlData)))
{
response = (Response)serializer.ReadObject(ms);
}
//This works
Assert.AreEqual("12345", response.Key);
//This works
Assert.AreEqual("25", response.Property.Address.StreetNumber);
//This FAILS. StreetAddress is null
Assert.AreEqual("ELM ST", response.Property.Address.StreetAddress);
}
私の人生では、StreetAddress が失敗する理由がわかりません。私が見逃しているのは単純なものでなければなりません。