Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3322138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:08:47+00:00 2026-05-17T23:08:47+00:00

Just for some fun I was playing with the API of Last.fm. The XML

  • 0

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">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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:08:47+00:00Added an answer on May 17, 2026 at 11:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing around some in C++, just for fun. I have just begun
For just some fun i am trying to use Flat File databases to read
Ok so I am just trying to learn some of this fun stuff now,
I'm new to jQuery and just playing for fun. I have some code that
So, I was just playing around making some page for fun. I cannot seem
I'm writing some code (just for fun so far) in Python that will store
I built (just for fun) 3 classes to help me log some events in
this is just some pretty standard code I have tried. What I am trying
I was just playing around with cpplint and tried running it on some code
I'm writing a simple QR code generator (just for fun and to learn some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.