使用XMLSerializer反序列化与元素数组相同的元素数组

时间:2011-05-31 09:26:03

标签: c# xml serialization mono xml-deserialization

我有以下XML我正在尝试使用XmlSerialiser进行反序列化:

<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”?

感谢。

3 个答案:

答案 0 :(得分:1)

请参阅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

答案 1 :(得分:0)

您仅使用XmlRootAttribute将PlaylistResponse标记为根元素。也添加XmlElementAttribute。

答案 2 :(得分:0)

添加XmlArrayItem属性以命名元素名称,添加XmlRoot以指定根元素的名称:

[XmlRoot("playlist")] 
public class PlaylistResponse : Response
{
    public int id;
    public string title;
    public string description;
    [XmlArrayItem("playlist")]
    public List<PlaylistResponse> childPlaylists;
}