开发者

Not being able to deserealize this XML

开发者 https://www.devze.com 2023-01-23 22:02 出处:网络
Just for some fun I was playing with the API of Last.fm. The XML file they return for top artists is structured like this:

Just for some fun I was playing with the API of Last.fm. The XML file they return for top artists is structured like this:

<lfm status="ok">
 <topartists user="xbonez" type="overall">
  <artist rank="1">
  <name>Evanescence</name> 
  <playcount>4618</playcount> 
  <mbid>f4a31f0a-51dd-4fa7-986d-3095c40c5ed9</mbid> 
  <url>http://www.last.fm/music/Evanescence</url> 
  <streamable>1</streamable> 
  <image size="small">http://userserve-ak.last.fm/serve/34/48488613.png</image> 
  <image size="medium"开发者_如何学Go>http://userserve-ak.last.fm/serve/64/48488613.png</image> 
  <image size="large">http://userserve-ak.last.fm/serve/126/48488613.png</image> 
  <image size="extralarge">http://userserve-ak.last.fm/serve/252/48488613.png</image> 
  <image size="mega">http://userserve-ak.last.fm/serve/500/48488613/Evanescence++PNG.png</image> 
  </artist>
 </topartists>
</lfm>

This is how I'm deserealizing it.

I have a class called lfmStatus:

    [Serializable()]
        [XmlRootAttribute("lfm")]
        public class lfmStatus
        {
            [XmlElement("artist")]
            public List<Artists> TopArtists { get; set; }         
        }

And a Class Artists:

[Serializable()]
    public class Artists
    {
        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("playcount")]
        public int playcount { get; set; }

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

        [XmlElement("streamable")]
        public int streamable { get; set; }

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

    }

And then I deserealize using this code:

string XmlFile = "artists.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(lfmStatus));

            lfmStatus LoadFile;

            using (Stream reader = new FileStream(XmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                try
                {
                    Console.WriteLine("Beginning deserialization.");

                    // Call the Deserialize method to restore the object's state.
                    LoadFile = (lfmStatus)serializer.Deserialize(reader);

                    return LoadFile.TopArtists;
                }

Now, this code works great for the XML if it did not have the topartists tag enveloping all the artists. But since it does, how do I change my code to handle that? I'm assuming I need to add another class.


You are missing the attribute(s) on a few types.

See XmlAttributeAttribute for more detail.

You are also missing the topartists element's type.

If I was you, I would get the XML schema and just use xsd.exe to generate the C# classes, and modify from there. It can also infer the schema based on XML if you really can't find it, this will give you a parsable result based on the input XML.


To see that you wrote correct code for deserializing the response XML you can use XSD. Open VS command prompt and give XSD LastFM.xml which generates and XSD file. Now give XSD LastFM.XSD which will generate a CS file. compare that one with the one you have written and check if you made any mistakes.

0

精彩评论

暂无评论...
验证码 换一张
取 消