Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialization returns empty objects
    primarykey
    data
    text
    <p>I want to desirialize an XML file to C# Objects. My objects are as follows</p> <pre><code>[Serializable] [XmlRoot(ElementName = "Collection")] public class Collection { public Collection() { Artiesten = new List&lt;Artiest&gt;(); Albums = new List&lt;Album&gt;(); Nummers = new List&lt;Nummer&gt;(); } [XmlElement("Artiesten")] public List&lt;Artiest&gt; Artiesten { get; set; } [XmlElement("Albums")] public List&lt;Album&gt; Albums { get; set; } [XmlElement("Nummers")] public List&lt;Nummer&gt; Nummers { get; set; } } [Serializable] public class Artiest { [XmlAttribute("artiestid")] public int ArtiestId { get; set; } [XmlElement(ElementName = "Naam")] public String Naam { get; set; } [XmlElement(ElementName = "Albums")] public List&lt;Album&gt; Albums { get; set; } } [Serializable] public class Nummer { [XmlAttribute("nummerid")] public int NummerId { get; set; } [XmlElement(ElementName = "titel")] public String Titel { get; set; } [XmlElement(ElementName = "duur")] public String Duration { get; set; } } </code></pre> <p>My XML is this:</p> <pre><code>&lt;Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Artiesten&gt; &lt;Artiest artiestid="1"&gt; &lt;Naam&gt;Harry&lt;/Naam&gt; &lt;Albums&gt; &lt;Album albumid="1"&gt; &lt;Titel&gt;Album1&lt;/Titel&gt; &lt;prijs valuta="Euro"&gt;19.99&lt;/prijs&gt; &lt;uitgiftejaar&gt;1999&lt;/uitgiftejaar&gt; &lt;Nummers&gt; &lt;Nummer nummerid="1"&gt; &lt;titel&gt;happy Sundays&lt;/titel&gt; &lt;duur&gt;PT02M02S&lt;/duur&gt; &lt;/Nummer&gt; &lt;/Nummers&gt; &lt;/Album&gt; &lt;/Albums&gt; &lt;/Artiest&gt; &lt;/Artiesten&gt; &lt;Albums&gt; &lt;Album albumid="1"&gt; &lt;Titel&gt;Album1&lt;/Titel&gt; &lt;prijs valuta="Euro"&gt;19.99&lt;/prijs&gt; &lt;uitgiftejaar&gt;1999&lt;/uitgiftejaar&gt; &lt;Nummers&gt; &lt;Nummer nummerid="1"&gt; &lt;titel&gt;Happy Sundays&lt;/titel&gt; &lt;duur&gt;PT02M02S&lt;/duur&gt; &lt;/Nummer&gt; &lt;/Nummers&gt; &lt;/Album&gt; &lt;/Albums&gt; &lt;Nummers&gt; &lt;Nummer nummerid="1"&gt; &lt;titel&gt;Happy Sundays&lt;/titel&gt; &lt;duur&gt;PT02M02S&lt;/duur&gt; &lt;/Nummer&gt; &lt;/Nummers&gt; &lt;/Collection&gt; </code></pre> <p>And I'm trying to desirialize like this:</p> <pre><code>XDocument doc = XDocument.Load(file); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Collection)); using (var reader = doc.Root.CreateReader()) { Collection collection = (Collection) xmlSerializer.Deserialize(reader); } </code></pre> <p>For some reason I can't find the lists in the <code>Collection</code> object are all empty. Debugging shows that the loaded file in <code>XDocument</code> is valid.</p> <p>EDIT: I managed to narrow down the problem. It does deserialize the lists correctly, only all property's of the Objects in those lists are empty.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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