must declare a body because it is not marked abstract or extern

Hi I am a newbie and am getting this error when trying to learn xml serialization

Error 1      'XMLSerialize.Movie.Title.get' must declare a body because it is not marked abstract or extern C:\XMLSerializer\XMLSerializer\Program.c

I have attached the code snippet, could anyone please tell me why and what to do to fix?

Thank You
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
 
namespace XMLSerialize
{
    public class MainClass
    {
 
        static void Main(string[] args)
        {
            Movie movie = new Movie();
            movie.Title = "Starship Troopers";
            movie.ReleaseDate = DateTime.Parse("11/7/1997");
            movie.Rating = 6.9f;
 
            SerializeToXML(movie);
        }
 
        static public void SerializeToXML(Movie movie)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Movie));
            TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
            serializer.Serialize(textWriter, movie);
            textWriter.Close();
        }
    }
 
 
    public class Movie
    {
        public string Title
        {
            get; set;
        }
 
        public int Rating
        { get; set; }
 
        public DateTime ReleaseDate
        { get; set; }
    }
}

Open in new window

Charles BaldoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
Looks like you're trying to use the auto-implemented properties, which were not released until c# 3.0.

If you're using 2.0, you still need to write out the full functionality of getting and setting the properties:
    public class Movie
    {
        private string _title;
        private int _rating;
        private DateTime _releaseDate;
 
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
 
        public int Rating
        {
            get { return _rating; }
            set { _rating = value; }
        }
 
        public DateTime ReleaseDate
        {
            get { return _releaseDate; }
            set { _releaseDate = value; }
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anurag ThakurTechnical ManagerCommented:
one more thing for .Net 2.0
initialize all your strings else they will not be serialized on serialization if the members are null
0
Fernando SotoRetiredCommented:
Hi charlesbaldo;

I found one error in the code you posted. You have this line in the Main subtoutine:

movie.Rating = 6.9f;

But the Move class you have it like this:

public int Rating  { get; set; }

I changed it like this:

public float Rating { get; set; }

And rand the code and it producted the XML file below.

Fernando

<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Starship Troopers</Title>
  <Rating>6.9</Rating>
  <ReleaseDate>1997-11-07T00:00:00</ReleaseDate>
</Movie>

Open in new window

0
Fernando SotoRetiredCommented:
Well that's what I get for using VS .Net 3.5. Snarf0001 is correct about auto-implemented properties. ;==)
0
Charles BaldoSoftware DeveloperAuthor Commented:
thanks a ton Snarf0001, Thanks fernando as well as catching data type mismatch
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.