Unable to deserialize an xml file

Go To StackoverFlow.com

1

I have an xml file like the following that I'm trying to deserialize.

<info>
  <result resultCode="0000"><![CDATA[操作成功]]></result>
  <songlist>
    <song>
      <id>63672</id>
      <name><![CDATA[红玫瑰]]></name>
      <singer id="1620"><![CDATA[陈奕迅]]></singer>
      <album id="22056"><![CDATA[认了吧]]></album>
      <remark><![CDATA[]]></remark>
      <uploadinguser><![CDATA[]]></uploadinguser>
      <collectinguser><![CDATA[]]></collectinguser>
      <source>
        <link id="3441591" filesize="3842715" format="mp3"><![CDATA[http://space6.j.cn/olympic/edit/672/63672-3842715.mp3]]></link>
        <link id="3435011" filesize="3843133" format="mp3"><![CDATA[http://f8.wretch.yimg.com/satyedhome/32764/1165646407.mp3]]></link>
        <link id="3434519" filesize="3842715" format="mp3"><![CDATA[http://space0.j.cn/olympic/edit/672/63672-3842715.mp3]]></link>
      </source>
    </song>
    <song>
      <id>67228</id>
      <name><![CDATA[光荣]]></name>
      <singer id="106"><![CDATA[BOBO]]></singer>
      <album id="22523"><![CDATA[光荣]]></album>
      <remark><![CDATA[]]></remark>
      <uploadinguser><![CDATA[]]></uploadinguser>
      <collectinguser><![CDATA[]]></collectinguser>
      <source>
        <link id="3437626" filesize="5106906" format="mp3"><![CDATA[http://blog.heinekenf1.net/music/gr.mp3]]></link>
        <link id="3441621" filesize="3394663" format="mp3"><![CDATA[http://space6.j.cn/olympic/edit/228/67228-3394663.mp3]]></link>
        <link id="3090938" filesize="3395499" format="mp3"><![CDATA[http://space5.j.cn/olympic/convert/228/67228-3395499.mp3]]></link>
      </source>
    </song>
    <song>...</song>
    <song>...</song>
    <song>...</song>
    <song>...</song>
    <song>...</song>
    <song>...</song>
  </songlist>
</info>

Currently this is what I have as Model:

[XmlRoot("info")]
public class Response
{
    [XmlElement("result")]
    public Result Results { get; set; }

    [XmlArray("songlist")]
    [XmlArrayItem("song", typeof(Song))]
    Song[] SongList { get; set; }
}
public class Result
{
    [XmlAttribute("resultCode")]
    public int ResultCode { get; set; }
}
public class Song
{
    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("singer")]
    public string Artist { get; set; }

    [XmlElement("album")]
    public string Album { get; set; }

    [XmlArray("source")]
    [XmlArrayItem("link", typeof(Link))]
    public Link[] Sources { get; set; }
}
public class Link
{
    [XmlAttribute("filesize")]
    public int FileSize { get; set; }
    [XmlAttribute("format")]
    public string Format { get; set; }
    [XmlText]
    public string URI { get; set; }
}

But when I tried to deserialize using the code below, it doesn't get parsed right, i.e. I don't see the resultCode nor the song list (no error though).

XmlSerializer s = new XmlSerializer(typeof(Response), new XmlRootAttribute("info"));
Response response = (Response)s.Deserialize(data.CreateReader());

Any hint?

2012-04-05 16:44
by Geekhuh
Please don't add things like "C# 4.0" to the ends of your titles. That's what the tags are for. Also, your question is not specific to a particular version of C#, so you don't need to specifyn. the version - John Saunders 2012-04-05 16:46
what is you data object ? What does .CreateReader do ? Does it return a XmlTextReader - Baptiste Pernet 2012-04-05 16:50
The only advice I can offer you is to try and deserialize it piece by piece, you are doing a lot in one go there and it will be hard to track down whats not working. Thats how I approached similar problems in the past anyway - DukeOfMarmalade 2012-04-05 17:03
@JohnSaunders Sorry about that, I'm new to the forum, will keep that in mind in the future - Geekhuh 2012-04-05 21:25
The data object is an XDocument @Jim I did try to serialize just the Result, but it didn't work eithe - Geekhuh 2012-04-05 21:26
Your missing a /song on one of your song nodes - LightLabyrinth 2012-04-05 16:52
I hand edited it, sorry :p It should be fixed now - Geekhuh 2012-04-05 21:25


4

First ensure that all properties are public, as serialization only takes public properties. Your Song[] is without an access modifier and is defaulted to private.

Use this as a start to deserialize your xml. There were a few changes I made to get it to work. E.g. Creating an element SongList, using XmlElement rather than XmlArray.

[XmlRoot("info")]
public class Response
{
    [XmlElement("result")]
    public Result Result { get; set; }

    [XmlElement("songlist")]
    public SongList SongList { get; set; }
}

public class Result
{
    [XmlAttribute("resultCode")]
    public int ResultCode { get; set; }

    [XmlText]
    public string Value { get; set; }
}

public class SongList
{
    [XmlElement("song")]
    public Song[] Songs { get; set; }
}

public class Song
{
    [XmlElement("id")]
    public string Id { get; set; }
}

Hope that helps!

2012-04-05 23:52
by KXLight
Ads