Click here to Skip to main content
14,982,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'v got a calendar/appointment winform application and i want to save all data from textboxes and comboboxes with a save button and load all data from a xml file, and i have got 3 forms: the main form, the appointment form and the recurring appointment form. The appointment and the recurring appointment forms open with 2 separate buttons.

this is my way but i get this error when i press on the save button : Calendar.MyData is inaccessible due to its protection level. Only public types can be processed. on this line of code :
XML
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List<MyData>));


XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Calendar
{
    class MyFiler
    {
        // Load any saved data.
        public static List<MyData> ReadXML()
        {
            List<MyData> data = new List<MyData>();
            System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(List<MyData>));

            if (File.Exists(@"MySavedFile.xml"))
            {
                System.IO.StreamReader file = new System.IO.StreamReader(@"MySavedFile.xml");
                data = (List<MyData>)reader.Deserialize(file);
                file.Close();
            }

            return data;
        }

        // Save all data.
        public static void WriteXML(List<MyData> data)
        {
            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List<MyData>));

            System.IO.StreamWriter file = new System.IO.StreamWriter(@"MySavedFile.xml");
            writer.Serialize(file, data);
            file.Close();
        }

        internal static void WriteXml(object Data)
        {
            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List<MyData>));

            System.IO.StreamWriter file = new System.IO.StreamWriter(@"MySavedFile.xml");
            writer.Serialize(file, data);
            file.Close();
        }

        internal static object ReadXml()
        {
            throw new NotImplementedException();
        }

        public static object data { get; set; }
    }
}



and this

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Calendar
{
    class MyData
    {
        [XmlElement]
        public string Location { get; set; }

        [XmlElement]
        public string Subject { get; set; }
    }
}


C#
private void NewAppointmentForm_Load(object sender, EventArgs e)
        {
            Data = MyFiler.ReadXml();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyFiler.WriteXml(Data);
        }


public object Data { get; set; }




Any answers are appreciated!
Posted
Updated 29-Apr-15 2:08am
v2
Comments
Florian Braun 29-Apr-15 7:44am
   
what did you try so far? any code we can help with?

1 solution

Because you didn't specify an access modifier, it has defaulted to internal - which means that the class is only visible within the current assembly - and that means that it isn't visible inside the system assemblies for XML serialisation.
Declare the class as public and it should work:
C#
namespace Calendar
{
    public class MyData
    {
        [XmlElement]
   
Comments
Member 11649865 29-Apr-15 8:23am
   
i tried that but when i do that then the save button does not work anymore for some reason!
OriginalGriff 29-Apr-15 8:35am
   
"does not work" is not a helpful error message! :laugh:

Remember that we can't see your screen, access your HDD, or read your mind - you need to tell us what does (or doesn't) happen...
Member 11649865 29-Apr-15 8:42am
   
after i make my class public, the save button is "disabled", like an empty button_Click.
OriginalGriff 29-Apr-15 10:29am
   
You need to look at your button click handler: changing the access modifier won;t disable anything on it's own - it extends access not reduces it.
ConnectingKamlesh 29-Apr-15 9:47am
   
To resolve "Calendar.MyData is inaccessible due to its protection level. Only public types can be processed." error , you need to declare MyData class as public, as suggested by OriginalGriff, just let us know more about the save button, are you setting the disabled property somewhere ?
Member 11649865 29-Apr-15 10:00am
   
I am not setting something more. Just, when im setting my class public, and then run the application and press the save button, nothing happen. It is like you drag and drop a new button and you don't develop it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900