XmlException - Zapis do pliku xml obiektów

0

Witam.
Piszę program na zaliczenie z C#. Stworzyłem sobie obiekty Lekarzy różnych specjalności, zamierzam dodać metody modyfikujące i dodające i usuwające lekarzy.
Oprócz tego wpadłem na pomysł że program będzie na początku sobie wczytywał dane z plików xml a pod koniec działania zapisywał.
I tu pojawia się problem ponieważ funkcja zapisująca do xml wywala się wyrzucając komunikat o braku możliwości obsłużenia wyjątku.
Proszę o pomoc.
Program.cs

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


namespace JiPP___Projekt
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string curFile = @"c:\Dentysci.xml";
            string curFile2 = @"c:\Chirurdzy.xml";
            string curFile3 = @"c:\Pediatrzy.xml";

            List<Dentysta> ListaDentystow = null; 
            List<Pediatra> ListaPediatrow = null; 
            List<Chirurg> ListaChirurgow = null;

            

           if (File.Exists(curFile) && File.Exists(curFile2) && File.Exists(curFile3))
            {
                InputOutput.WczytajDane(ListaDentystow, ListaPediatrow, ListaChirurgow);
            }
            else
            {
                ListaDentystow = new List<Dentysta>();
                ListaPediatrow = new List<Pediatra>();
                ListaChirurgow = new List<Chirurg>();
            }
            

            ListaChirurgow.Add(new Chirurg(5, "Stfan","pal",5,5,5));
            ListaDentystow.Add(new Dentysta(5, "Stfan","pal",5,5,5));
            ListaPediatrow.Add(new Pediatra(5, "Stfan","pal",5,5,5));

            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();

            char wybor;
        menu:

            Console.WriteLine("                 ****************************************");
            Console.WriteLine("                 *  Łukasz Bartnicki D303               *\n                 *  Program Zaliczeniowy                *\n                 *  Języki i Paradygmaty Programowania  *");
            Console.WriteLine("                 ****************************************");
            Console.WriteLine("\n\n");

            Console.WriteLine("                 ****************************************");
            Console.WriteLine("                 *                MENU                  *");
            Console.WriteLine("                 ****************************************");
            Console.WriteLine("\n");
            Console.WriteLine("Wybierz typ lekarza którym chcesz się zająć:");
            Console.WriteLine(" 1.Lekarz Dentysta");
            Console.WriteLine(" 2.Lekarz Chirurg");
            Console.WriteLine(" 3.Lekarz Pediatra");
            Console.WriteLine(" 4.Wyświetlenie listy wszytkich lekarzy.");
            Console.WriteLine(" 5.Opuszczenie programu.");

            Console.WriteLine("Twój wybór: ");
            wybor = char.Parse(Console.ReadLine());

            switch (wybor)
            {
                case '1':
                    {
                        Console.WriteLine("Dentyści: ");
                        Console.WriteLine(" 1.Dodaj");
                        Console.WriteLine(" 2.Usuń");
                        Console.WriteLine(" 3.Edytuj");
                        Console.WriteLine(" 4.Wylistuj");
                        Console.WriteLine(" 5.Powrót do menu głownego");

                        Console.WriteLine("Twój wybór: ");
                        wybor = char.Parse(Console.ReadLine());

                        switch (wybor)
                        {
                            case '1':
                                {
                                    break;
                                }
                            case '2':
                                {
                                    break;
                                }
                            case '3':
                                {
                                    break;
                                }
                            case '4':
                                {
                                    Console.ReadKey();
                                    break;
                                }
                            case '5':
                                {
                                    break;
                                }
                        
                        }

                        Console.Clear();
                        goto menu;
                    }
                case '2':
                    {
                        Console.WriteLine("Chirurdzy: ");
                        Console.WriteLine(" 1.Dodaj");
                        Console.WriteLine(" 2.Usuń");
                        Console.WriteLine(" 3.Edytuj");
                        Console.WriteLine(" 4.Wylistuj");
                        Console.WriteLine(" 5.Powrót do menu głownego");

                        Console.WriteLine("Twój wybór: ");
                        wybor = char.Parse(Console.ReadLine());

                        switch (wybor)
                        {
                            case '1':
                                {
                                    break;
                                }
                            case '2':
                                {
                                    break;
                                }
                            case '3':
                                {
                                    break;
                                }
                            case '4':
                                {
                                    Console.ReadKey();
                                    break;
                                }
                            case '5':
                                {
                                    break;
                                }

                        }

                        Console.Clear();
                        goto menu;
                    }
                case '3':
                    {
                        Console.WriteLine("Pediatrzy: ");
                        Console.WriteLine(" 1.Dodaj");
                        Console.WriteLine(" 2.Usuń");
                        Console.WriteLine(" 3.Edytuj");
                        Console.WriteLine(" 4.Wylistuj");
                        Console.WriteLine(" 5.Powrót do menu głownego");

                        Console.WriteLine("Twój wybór: ");
                        wybor = char.Parse(Console.ReadLine());

                        switch (wybor)
                        {
                            case '1':
                                {
                                    break;
                                }
                            case '2':
                                {
                                    break;
                                }
                            case '3':
                                {
                                    break;
                                }
                            case '4':
                                {
                                    Console.ReadKey();
                                    break;
                                }
                            case '5':
                                {
                                    break;
                                }

                        }

                        Console.Clear();
                        goto menu;
                    }
                case '4':
                    {
                        foreach(Dentysta osoba in ListaDentystow)
                        {
                            Console.WriteLine("Id: {0}\nImie: {1}\nNazwisko: {3}\nWiek: {4}\nStaz: {4}\nCena leczenia zęba: {5}", osoba.Id, osoba.Imie, osoba.Nazwisko, osoba.Wiek, osoba.Staz, osoba.CenaLeczeniaZeba);
                        }

                        foreach(Chirurg osoba in ListaChirurgow)
                        {
                            Console.WriteLine("Id: {0}\nImie: {1}\nNazwisko: {3}\nWiek: {4}\nStaz: {4}\nCena za godzine operacji: {5}", osoba.Id, osoba.Imie, osoba.Nazwisko, osoba.Wiek, osoba.Staz, osoba.CenaGodzinyOperacji);
                        }

                        foreach(Pediatra osoba in ListaPediatrow)
                        {
                            Console.WriteLine("Id: {0}\nImie: {1}\nNazwisko: {3}\nWiek: {4}\nStaz: {4}\nCena za pacjenta: {5}", osoba.Id, osoba.Imie, osoba.Nazwisko, osoba.Wiek, osoba.Staz, osoba.OplataZaPacjenta);
                        }

                        Console.ReadKey();
                        Console.Clear();
                        goto menu;
                    }
                case '5':
                    {
                        break;
                    }
            }

            InputOutput.ZapiszDane(ListaDentystow, ListaPediatrow, ListaChirurgow);
        }   
    }
}

Klasa.cs

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


namespace JiPP___Projekt
{
    class InputOutput // Klasa do obsługi i zapisu danych do pliku XML. Metody statyczne nie wymagają tworzenia obiektu.
    //Użycie technologii Linq to XML w celu odczytu i zapisu bazy obiektów Lekarzy do plików XML.
    {
        public static void WczytajDane(List<Dentysta> ListaDentystow, List<Pediatra> ListaPediatrow, List<Chirurg> ListaChirurgow)
        {
            XDocument xml = XDocument.Load("Dentysci.xml");
            ListaDentystow = (
                from dentysta in xml.Root.Elements("dentysta")
                select new Dentysta(
                    int.Parse(dentysta.Attribute("id").Value),
                    dentysta.Element("Imie").Value,
                    dentysta.Element("Nazwisko").Value,
                    int.Parse(dentysta.Element("Wiek").Value),
                    int.Parse(dentysta.Element("Staz").Value),
                    int.Parse(dentysta.Element("Cena leczenia zęba").Value)
                    )
                    ).ToList<Dentysta>();

            XDocument xml2 = XDocument.Load("Chirurdzy.xml");
            ListaChirurgow = (
                from chirurg in xml.Root.Elements("chirurg")
                select new Chirurg(
                    int.Parse(chirurg.Attribute("id").Value),
                   chirurg.Element("Imie").Value,
                    chirurg.Element("Nazwisko").Value,
                    int.Parse(chirurg.Element("Wiek").Value),
                    int.Parse(chirurg.Element("Staz").Value),
                    int.Parse(chirurg.Element("Cena godziny operacji").Value)
                    )
                    ).ToList<Chirurg>();

            XDocument xml3 = XDocument.Load("Pediatrzy.xml");
           ListaPediatrow = (
                from pediatra in xml.Root.Elements("pediatra")
                select new Pediatra(
                    int.Parse(pediatra.Attribute("id").Value),
                    pediatra.Element("Imie").Value,
                    pediatra.Element("Nazwisko").Value,
                    int.Parse(pediatra.Element("Wiek").Value),
                    int.Parse(pediatra.Element("Staz").Value),
                    int.Parse(pediatra.Element("Cena za pacjenta").Value)
                    )
                    ).ToList<Pediatra>();

            
        }

        public static void ZapiszDane(List<Dentysta> ListaDentystow, List<Pediatra> ListaPediatrow, List<Chirurg> ListaChirurgow)
        {
                
                XDocument xml = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("Lista dentystow z kolekcji"),
                new XElement("dentysci",
                    from Dentysta in ListaDentystow
                    orderby Dentysta.Nazwisko, Dentysta.Imie
                    select new XElement("Dentysta",
                        new XAttribute("Id", Dentysta.Id),
                        new XElement("imie", Dentysta.Imie),
                        new XElement("nazwisko", Dentysta.Nazwisko),
                        new XElement("wzrost", Dentysta.Wiek),
                        new XElement("Staz", Dentysta.Staz),
                        new XElement("Cena leczenia zeba", Dentysta.CenaLeczeniaZeba))
                    )
               );

                xml.Save(@"c:\Dentysci.xml");

                XDocument xml2 = new XDocument(
                   new XDeclaration("1.0", "utf-8", "yes"),
                   new XComment("Lista chirurgów z kolekcji"),
                   new XElement("dentysci",
                       from Chirurg in ListaChirurgow
                       orderby Chirurg.Nazwisko, Chirurg.Imie
                       select new XElement("Chirurg",
                           new XAttribute("Idl", Chirurg.Id),
                           new XElement("imie", Chirurg.Imie),
                           new XElement("nazwisko", Chirurg.Nazwisko),
                           new XElement("wzrost", Chirurg.Wiek),
                           new XElement("Staz", Chirurg.Staz),
                           new XElement("Cena godziny operacji", Chirurg.CenaGodzinyOperacji)
                           )
                       )
                  );

                xml.Save(@"c:\Chirurdzy.xml");

                XDocument xml3 = new XDocument(
                   new XDeclaration("1.0", "utf-8", "yes"),
                   new XComment("Lista pediatrów z kolekcji"),
                   new XElement("dentysci",
                       from Pediatra in ListaPediatrow
                       orderby Pediatra.Nazwisko, Pediatra.Imie
                       select new XElement("Dentysta",
                           new XAttribute("Id", Pediatra.Id),
                           new XElement("imie", Pediatra.Imie),
                           new XElement("nazwisko", Pediatra.Nazwisko),
                           new XElement("wzrost", Pediatra.Wiek),
                           new XElement("Staz", Pediatra.Staz),
                           new XElement("Cena leczenia zeba", Pediatra.OplataZaPacjenta)
                           )
                       )
                  );

                xml.Save(@"c:\Pediatrzy.xml");

                

        }
    }
    
    interface IDochod //Interfejs wymuszający na klasach dziedziczących po nim, definiowanie metowy LiczPensje(), w klasach Dentysta, Chirurg, Pediatra
    {
        double LiczPensje();
    }

    abstract class Lekarz //klasa abstrakcyjna służy tylko do dziedziczenia, nie tworzymy z niej obiektów
    {   //zmienne prywatne do ktorych dostęp będzie zapewniony przez właściwosci

        private int id;
        private string imie;
        private string nazwisko;
        private int wiek;
        private int staz;
        
        //własciwości zmiennych
        public int Id
        {
            set { this.id = value; }
            get { return this.id; }
        }

        public string Imie
        {
            set { this.imie = value; }
            get { return this.imie; }
        }

        public string Nazwisko
        {
            set { this.nazwisko= value; }
            get { return this.nazwisko; }
        }

        public int Wiek
        {
            set { this.wiek = value; }
            get { return this.wiek; }
        }

        public int Staz
        {
            set { this.staz = value; }
            get { return this.staz; }
        }

        //kontruktory
        public Lekarz()
        {
            this.Id = 0;
            this.Imie = "brak";
            this.Nazwisko = "brak";
            this.Wiek = 0;
            this.Staz = 0;
        }

        public Lekarz(int id, string imie, string nazwisko, int wiek, int staz)
        {
            this.Id = id;
            this.Imie = imie;
            this.Nazwisko = nazwisko;
            this.Wiek = wiek;
            this.Staz = staz;
        }

        public virtual void Wyswietl()
        {
            Console.WriteLine("ID Lekarza: {0}\n", this.Id);
            Console.WriteLine("Imie Lekarza: {0}\n", this.Imie);
            Console.WriteLine("Nazwisko Lekarza: {0}\n", this.Nazwisko);
            Console.WriteLine("Wiek Lekarza: {0}\n", this.Wiek);
            Console.WriteLine("Staż Lekarza: {0}\n", this.Staz);
        }

     }

    class Dentysta : Lekarz, IDochod
    {
        private int cenaleczeniazeba;

        public int CenaLeczeniaZeba
        {
            set { this.cenaleczeniazeba = value; }
            get { return this.cenaleczeniazeba; }
        }

        //kontruktory
        public Dentysta()
            : base()
        {
            this.CenaLeczeniaZeba = 0;
        }

        public Dentysta(int id, string imie, string nazwisko, int wiek, int staz, int CenaLeczeniaZeba)
            : base(id, imie, nazwisko, wiek, staz)
        {
            this.CenaLeczeniaZeba = CenaLeczeniaZeba;
        }

        public override void Wyswietl()
        {
            base.Wyswietl();
            Console.WriteLine("Cena za leczenie pojedyńczego zęba: {0}\n", this.CenaLeczeniaZeba);
        }
        
        //Metoda wymuszona przez interfejs
        public double LiczPensje()
        {
            return 0;
        }
    }

    class Pediatra : Lekarz, IDochod
    {
        private int oplatazapacjenta;

        public int OplataZaPacjenta
        {
            set { this.oplatazapacjenta = value; }
            get { return this.oplatazapacjenta; }
        }
        //kontruktory
        public Pediatra()
            : base()
        {
            this.OplataZaPacjenta = 0;
        }

        public Pediatra(int id, string imie, string nazwisko, int wiek, int staz, int CenaLeczeniaZeba)
            : base(id, imie, nazwisko, wiek, staz)
        {
            this.OplataZaPacjenta = OplataZaPacjenta;
        }

        public override void Wyswietl()
        {
            base.Wyswietl();
            Console.WriteLine("Cena za leczenie pojedyńczego pacjenta: {0}\n", this.OplataZaPacjenta);
        }

        //Metoda wymuszona przez interfejs
        public double LiczPensje()
        {
            return 0;
        }
    }

    class Chirurg : Lekarz, IDochod
    {
        private int cenagodzinyoperacji;

        public int CenaGodzinyOperacji
        {
            set { this.cenagodzinyoperacji = value; }
            get { return this.cenagodzinyoperacji; }
        }
        //kontruktory
        public Chirurg()
            :base()
        {
            this.CenaGodzinyOperacji = 0;
        }

        public Chirurg(int id, string imie, string nazwisko, int wiek, int staz, int CenaLeczeniaZeba)
            : base(id, imie, nazwisko, wiek, staz)
        {
            this.CenaGodzinyOperacji = CenaGodzinyOperacji;
        }

        public override void Wyswietl()
        {
            base.Wyswietl();
            Console.WriteLine("Cena za godzine operowania: {0}\n", this.CenaGodzinyOperacji);
        }
        
        //Metoda wymuszona przez interfejs
        public double LiczPensje()
        {
            return 0;
        }
    }
   
}

1

Nie lepiej zrobić normalną wbudowaną serializację?

0
NewbieMaster napisał(a):
 interface IDochod //Interfejs wymuszający na klasach dziedziczących po nim, definiowanie metowy LiczPensje(), w klasach Dentysta, Chirurg, Pediatra
    {
        double LiczPensje();
    }

klasy nie dziedziczą po interfejsach, tylko je implementują ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1