简体   繁体   中英

Serialize a class using XML

After watching more than 3 hours of tutoring I can't find an answer:

I'm trying to serialize a class here:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Gestionnaire_d_Établissement_Scolaire
{
    public class Resources
    {
        public List<Student> Students { get; set; }
        public List<Teacher> Teachers { get; set; }
        public List<Etablissement> Etablissements { get; set; }

        public string Path_Etablissement = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Etablissements.xml";
        public string Path_Teacher = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Teachers.xml";
        public string Path_Student = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Students.xml";

        public void LoadAll()
        {

        }

        public void AddTeacher()
        {

        }

        public void AddEtablissement()
        {
            var doc = new XDocument();
            using (var writer = doc.CreateWriter())
            {
                var serializer = new XmlSerializer(typeof(Resources));

                serializer.Serialize(writer, this);
            }

            doc.Save(Path_Etablissement);
        }
    }
}
}

But this code gives me an error as follows:

System.IO.FileLoadException: The name or base code of the given assembly is not valid. (HRESULT exception: 0x80131047)

Frenziedly I am lost and I have no solution. It's at the line:

                var serializer = new XmlSerializer(typeof(Resources));

Thank you for helping me


EDIT

And if you - you ask yourself it's only the 3 lists I want to serialize, but it really doesn't matter if the path variables are also taken

Try using a GET :

        public string Path_Etablissement
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Etablissements.xml"; }
        }
        public string Path_Teacher
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Teachers.xml"; }
        }
        public string Path_Student
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Students.xml"; }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM