Erik Deijl
Erik Deijl

Reputation: 154

Deserialization returns empty objects

I want to desirialize an XML file to C# Objects. My objects are as follows

[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
    public Collection() 
    {
        Artiesten = new List<Artiest>();
        Albums = new List<Album>();
        Nummers = new List<Nummer>();
    }
    [XmlElement("Artiesten")]
    public List<Artiest> Artiesten { get; set; }
    [XmlElement("Albums")]
    public List<Album> Albums { get; set; }
    [XmlElement("Nummers")]
    public List<Nummer> 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<Album> 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; }
}

My XML is this:

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Artiesten>
    <Artiest artiestid="1">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
    </Artiest>
  </Artiesten>
  <Albums>
    <Album albumid="1">
      <Titel>Album1</Titel>
      <prijs valuta="Euro">19.99</prijs>
      <uitgiftejaar>1999</uitgiftejaar>
      <Nummers>
        <Nummer nummerid="1">
          <titel>Happy Sundays</titel>
          <duur>PT02M02S</duur>
        </Nummer>
      </Nummers>
    </Album>
  </Albums>
  <Nummers>
    <Nummer nummerid="1">
      <titel>Happy Sundays</titel>
      <duur>PT02M02S</duur>
    </Nummer>
  </Nummers>
</Collection>

And I'm trying to desirialize like this:

XDocument doc = XDocument.Load(file);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Collection));
                using (var reader = doc.Root.CreateReader())
                {
                    Collection collection = (Collection) xmlSerializer.Deserialize(reader);

                }

For some reason I can't find the lists in the Collection object are all empty. Debugging shows that the loaded file in XDocument is valid.

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.

Upvotes: 0

Views: 1650

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1062800

You want XmlArray, not XmlElement:

[XmlArray("Artiesten")]
[XmlArrayItem("Artiest")]
public List<Artiest> ...

Actually this is the default behaviour for lists, so you also just remove the attribute completely.

Upvotes: 0

Erik Deijl
Erik Deijl

Reputation: 154

Found my answer

I had to edit My list Attributes to this:

    [XmlElement("Artiesten", typeof(List<Artiest>))]
    public List<Artiest> Artiesten { get; set; }
    [XmlElement("Albums", typeof(List<Album>))]
    public List<Album> Albums { get; set; }
    [XmlElement("Nummers", typeof(List<Nummer>))]
    public List<Nummer> Nummers { get; set; }

Upvotes: 1

Chris Sinclair
Chris Sinclair

Reputation: 23208

You need to re move the [XmlElement] tags from your lists. Otherwise it works with a different XML structure.

For example, rather than nest all your Artiest objects within a single Artiesten element (which is what your current XML is), it will actually set them adjacent to each other like this:

  <Artiesten artiestid="1">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>
  <Artiesten artiestid="2">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>
  <Artiesten artiestid="3">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>

So try redefining your classes as such:

[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
    public Collection() 
    {
        Artiesten = new List<Artiest>();
        Albums = new List<Album>();
        Nummers = new List<Nummer>();
    }

    public List<Artiest> Artiesten { get; set; }
    public List<Album> Albums { get; set; }
    public List<Nummer> Nummers { get; set; }

}

[Serializable]
public class Artiest
{
    [XmlAttribute("artiestid")]
    public int ArtiestId { get; set; }
    [XmlElement(ElementName = "Naam")]
    public String Naam { get; set; }

    public List<Album> Albums { get; set; } 
}

Upvotes: 0

MineScript
MineScript

Reputation: 321

Collection collection = null;
string path = "file.xml";

XmlSerializer serializer = new XmlSerializer(typeof(Collection));

StreamReader reader = new StreamReader(path);
collection = (Collection)serializer.Deserialize(reader);
reader.Close();

Upvotes: 0

Related Questions