Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing XML with DataContractSerializer
    text
    copied!<p>I have a web service that returns the following data:</p> <pre><code>&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt; &lt;RESPONSE&gt; &lt;KEY&gt;12345&lt;/KEY&gt; &lt;PROPERTY&gt; &lt;PROPERTY_ADDRESS&gt; &lt;STREET_NUM&gt;25&lt;/STREET_NUM&gt; &lt;STREET_ADDRESS&gt;ELM ST&lt;/STREET_ADDRESS&gt; &lt;STREET_PREFIX/&gt; &lt;STREET_NAME&gt;ELM&lt;/STREET_NAME&gt; &lt;STREET_TYPE&gt;ST&lt;/STREET_TYPE&gt; &lt;STREET_SUFFIX/&gt; &lt;/PROPERTY_ADDRESS&gt; &lt;/PROPERTY&gt; &lt;/RESPONSE&gt; </code></pre> <p>I have a class structure to match:</p> <pre><code>[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; } } </code></pre> <p>My deserialization code looks like this:</p> <pre><code>[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); } </code></pre> <p>For the life of me I can't figure out why StreetAddress is failing. It's got to be something simple that I'm missing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload