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?
 
Snarf0001Connect With a Mentor Commented:
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
 
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 SotoConnect With a Mentor RetiredCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.