I've got the following XML i'm trying to deserialise with XmlSerialiser:
<playlist>
<id>43712</id>
<title>Eleven | Eleven</title>
<description>Eleven</description>
<creationDate>2010-12-01T17:54:44</creationDate>
<lastModifiedDate&g开发者_StackOverflow社区t;2011-05-27T01:32:58</lastModifiedDate>
<status>ACTIVE</status>
<syndicatedPartners/>
<mediaList/>
<childPlaylists>
<playlist>
<id>43781</id>
<title>TV Shows | TV Shows</title>
<description></description>
<creationDate>2010-12-10T16:37:00</creationDate>
<lastModifiedDate>2011-05-09T06:03:09</lastModifiedDate>
<status>ACTIVE</status>
<syndicatedPartners/>
<imagePath>http://media.movideo.com/images/112/playlist/43781/</imagePath>
<defaultImage>
<url>http://media.movideo.com/images/112/playlist/43781/96x128.png</url>
<height>128</height>
<width>96</width>
</defaultImage>
</playlist>
<playlist>
<id>43787</id>
<title>Movies | Movies</title>
<description></description>
<creationDate>2010-12-10T16:49:13</creationDate>
<lastModifiedDate>2011-05-06T01:39:23</lastModifiedDate>
<status>ACTIVE</status>
<syndicatedPartners/>
<imagePath>http://media.movideo.com/images/112/playlist/43787/</imagePath>
<defaultImage>
<url>http://media.movideo.com/images/112/playlist/43787/96x128.png</url>
<height>128</height>
<width>96</width>
</defaultImage>
</playlist>
<playlist>
<id>44020</id>
<title>Eleven | This Week On Eleven</title>
<description></description>
<creationDate>2011-01-05T08:17:09</creationDate>
<lastModifiedDate>2011-05-30T08:47:13</lastModifiedDate>
<status>ACTIVE</status>
<syndicatedPartners/>
<imagePath>http://media.movideo.com/images/112/playlist/44020/</imagePath>
<defaultImage>
<url>http://media.movideo.com/images/112/playlist/44020/96x128.png</url>
<height>128</height>
<width>96</width>
</defaultImage>
</playlist>
<playlist>
<id>44758</id>
<title>Fashion | Australian Fashion Week</title>
<description></description>
<creationDate>2011-04-28T23:58:01</creationDate>
<lastModifiedDate>2011-05-06T05:48:22</lastModifiedDate>
<status>ACTIVE</status>
<syndicatedPartners/>
<imagePath>http://media.movideo.com/images/140/playlist/44758/</imagePath>
<defaultImage>
<url>http://media.movideo.com/images/140/playlist/44758/96x128.png</url>
<height>128</height>
<width>96</width>
</defaultImage>
</playlist>
</childPlaylists>
<imagePath>http://media.movideo.com/images/112/playlist/43712/</imagePath>
<defaultImage>
<url>http://media.movideo.com/images/112/playlist/43712/96x128.png</url>
<height>128</height>
<width>96</width>
</defaultImage>
</playlist>
In my C# code, I have:
public class Response
{
public bool success;
public string responseType;
}
[XmlRootAttribute("playlist", Namespace="", IsNullable=false)]
public class PlaylistResponse : Response
{
public int id;
public string title;
public string description;
public List<PlaylistResponse> childPlaylists;
}
XmlReader reader = XmlReader.Create(responseStream);
XmlSerializer serializer = new XmlSerializer(typeof(PlaylistResponse));
PlaylistResponse response = (PlaylistResponse)serializer.Deserialize(reader);
state.callback((Response) response, state.data);
but childPlaylist seems to always be empty.
What is the correct way of deserialising this sort of XML, specifically with multiple "childPlaylists"?
Thanks.
See this question and answer.
[XmlRootAttribute("playlist")]
public class PlaylistResponse
{
public int id;
public string title;
public string description;
[XmlArray(ElementName="childPlaylists")]
[XmlArrayItem(typeof(PlaylistResponse), ElementName="playlist")]
public PlaylistResponse[] ChildPlaylists;
}
XmlReader reader = XmlReader.Create(new StringReader(xml)); // your xml above
XmlSerializer serializer = new XmlSerializer(typeof(PlaylistResponse));
PlaylistResponse response = (PlaylistResponse)serializer.Deserialize(reader);
int count = response.ChildPlaylists.Length; // 4
You marked PlaylistResponse as root element only with XmlRootAttribute. Add XmlElementAttribute as well.
Add the XmlArrayItem attribute to name the element names and XmlRoot to specify the name of the root element:
[XmlRoot("playlist")]
public class PlaylistResponse : Response
{
public int id;
public string title;
public string description;
[XmlArrayItem("playlist")]
public List<PlaylistResponse> childPlaylists;
}
精彩评论