问题描述
|
我正在尝试使用XmlSerialiser对以下XML进行反序列化:
<playlist>
<id>43712</id>
<title>Eleven | Eleven</title>
<description>Eleven</description>
<creationDate>2010-12-01T17:54:44</creationDate>
<lastModifiedDate>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>
在我的C#代码中,我有:
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);
但是childplaylist似乎总是空的。
反序列化这种XML(特别是使用多个\“ childplaylists \”)的正确方法是什么?
谢谢。
解决方法
请参阅此问题和答案。
[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
, 您仅使用XmlRootAttribute将PlaylistResponse标记为根元素。也添加XmlElementAttribute。
, 添加XmlArrayItem属性以命名元素名称,并添加XmlRoot以指定根元素的名称:
[XmlRoot(\"playlist\")]
public class PlaylistResponse : Response
{
public int id;
public string title;
public string description;
[XmlArrayItem(\"playlist\")]
public List<PlaylistResponse> childPlaylists;
}