Can't deserialize xml into subclasses

53 views
Skip to first unread message

Zac Smith

unread,
Mar 6, 2012, 3:35:40 AM3/6/12
to rest...@googlegroups.com
I am trying to deserialize the following xml (abridged):

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <result page="1" perPage="10" count="1">
    <document>
      <track>
        <genre id="37">
          <name>Rock</name>
        </genre>
</track> </document> </result> </response>
My DTO classes look like this:
public class Track
{
public string Name { get; set; }
public Genre Genre { get; set; }
}

public class Genre
{
public string Name { get; set; }
}

The track Name property gets set, but the genre Name property does not.

Can anyone tell me why this is?

Cristovão Morgado

unread,
Mar 6, 2012, 4:00:53 AM3/6/12
to Zac Smith, rest...@googlegroups.com
Try to put the id as an element <id>55</id>

Sent from my Lumia 800

From: Zac Smith
Sent: 06/03/2012 08:35
To: rest...@googlegroups.com
Subject: Can't deserialize xml into subclasses

Zac Smith

unread,
Mar 6, 2012, 12:15:12 PM3/6/12
to rest...@googlegroups.com
I don't have control of the xml format so I can't do that.

John Sheehan

unread,
Mar 6, 2012, 11:30:15 PM3/6/12
to rest...@googlegroups.com
are you using RootElement? also i don't see a track name anywhere in the sample data. am i missing something?

I don't know why Track.Name is being populated, only Genre.Name should be with the class defs you have. It might work with this (assuming root element is set to Track):

        public class Track
{

public Genre Genre { get; set; }
}

public class Genre
{
public string Name { get; set; }
            public int Id { get; set; }
}

Zac Smith

unread,
Mar 7, 2012, 12:26:30 AM3/7/12
to rest...@googlegroups.com
Yeah sorry the xml should have been:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <result page="1" perPage="10" count="1">
    <document>
      <track>
        <name>Someone New</name>

  <genre id="37"> <name>Rock</name>
        </genre>
</track> </document> </result> </response>

I tried setting the RootElement to 'track', but when I did that I didn't even get the Track.Name property set.

Zac Smith

unread,
Mar 9, 2012, 7:18:44 PM3/9/12
to rest...@googlegroups.com
My issue was that I had declared the DTO classes inside of another class. In my application I already have a Track class so I didn't want the DTO track class to interfere.

Is there anyway I can rename the DTO class to something other than Track?
Reply all
Reply to author
Forward
0 new messages