Locked xml Serialzer to file

  • Tuesday, February 28, 2012 11:34 PM
     
     

    Here is my code. Can someone please help me on this its compile but it doesn't run. Its shows an error "Chapter17_Exe2.Form1.Movie cannot be serialized because it does not have a parameterless constructor." Can someone please correct me what i have done wrong.

    thanks

    Noll

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml.Serialization;

    namespace Chapter17_Exe2
    {
        public partial class Form1 : Form
        {
            Movie[] movie = new Movie[10];

            public class Movie
            {
                //create a constrator
                private string title;
                private string director;
                private int year;

                public string Title{get; set;}
                public string Director{get; set;}
                public int Year { get; set; }

                public Movie(string t, string d, int y)
                {
                    title = t;
                    director = d;
                    year = y;
                }
             }

            public Form1()
            {
                InitializeComponent();

            }

            private void btnSave_Click(object sender, EventArgs e)
            {

                XmlSerializer serializer = new XmlSerializer(typeof(Movie));
                TextWriter textWriter = new StreamWriter(@"C:\Users\ALCABONE\Desktop\Movie xml");
                serializer.Serialize(textWriter, movie);
                textWriter.Close();

            }

            private void btnExit_Click(object sender, EventArgs e)
            {

                this.Close();
            }

            private void btnClear_Click(object sender, EventArgs e)
            {
                txtTitle.Clear();
                txtTitle.Clear();
                txtYear.Clear();
            }

            private void btnAdd_Click(object sender, EventArgs e)
            {
                txtTitle.Clear();
                txtDirector.Clear();
                txtYear.Clear();
            }

        }

    }

                                                           

All Replies

  • Thursday, March 01, 2012 12:27 PM
     
      Has Code

    Hi,

    To Serialize you should have a parameter less constructor with XML Serlization

    Try below code

    public class Movie { //create a constrator private string title; private string director; private int year; public string Title{get; set;} public string Director{get; set;} public int Year { get; set; }

    [Obsolete("For XML Serialization Only", true)] public Movie() { } public Movie(string t, string d, int y) { title = t; director = d; year = y; } }

    Thanks,




    Welcome to MSDN Forums. Feel free to ask your questions and Please Note to Vote helpful topics and Mark answering posts. Sudhakar