如何保存和加载? [英] How Do I Save And Load?

查看:106
本文介绍了如何保存和加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历/约会winform应用程序,我想保存文本框和组合框中的所有数据和一个保存按钮并加载来自xml文件的所有数据,我有3个表单:主表单,约会表格和经常性的预约表格。预约和定期预约表单打开,有2个单独的按钮。



这是我的方式,但是当我按下保存按钮时出现此错误:Calendar.MyData is由于其保护级别无法访问。只能处理公共类型。在这行代码中:

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 :

System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(List<MyData>));







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

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; }
    }
}







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!

推荐答案

因为您没有指定访问修饰符,它默认为 internal - 这意味着该类只能在当前程序集 - 这意味着它在XML序列化的系统程序集中是不可见的。

将类声明为 public 它应该工作:

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:
namespace Calendar
{
    public class MyData
    {
        [XmlElement]


这篇关于如何保存和加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆