Hi,
momentan versuche ich mich an Serialisierung, also an auslagerung von Daten in XML-Dateien.
Problem:
Bei meinem Programm (Quellcode folgt gleich) tritt ein Fehler auf, der nicht weiter erklärt wird. Ich kann zwar ausmachen, an welcher Stelle er genau auftritt, aber nicht warum.
Deswegen wollte ich mal fragen, ob jemand anderes den Fehler findet, denn je mehr Augen, desto eher werden Fehler entdeckt.
Program.cs
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace zavTarot
{
class Program
{
static XmlSerializer xmlSer;
static FileStream myStream;
static void Main(string[] args)
{
ArrayList cards = new ArrayList();
xmlSer = new XmlSerializer(typeof(ArrayList));
SerializeObject(cards, "Cards.dat");
Menu theMenu = new Menu();
bool stop = false;
string enter;
while (!stop)
{
Console.Clear();
theMenu.Start();
enter= Console.ReadLine();
switch (enter)
{
case "1":
cards = DeserializeObject("Cards.dat");
cards.Add(theMenu.NewCard());
SerializeObject(cards, "Cards.dat");
break;
case "6":
stop = true;
break;
default:
break;
}
}
}
/// <summary>
/// Exportieren / Speichern der Datenbank
/// </summary>
/// <param name="obj">Dieses Objekt soll exportiert werden.</param>
/// <param name="direction">Dorthin soll exportiert werden.</param>
public static void SerializeObject(ArrayList aL, string direction)
{
myStream = new FileStream(direction, FileMode.Create);
xmlSer.Serialize(myStream, aL); // Hier tritt der Fehler auf.
myStream.Close();
}
/// <summary>
/// Importieren der Datenbank
/// </summary>
/// <param name="direction">Dort ist die zu importierende Datei abgespeichert.</param>
/// <returns>Gibt das zu importierende Objekt zurück.</returns>
public static ArrayList DeserializeObject(string direction)
{
FileStream fs = new FileStream(direction, FileMode.Open);
ArrayList tempArLi = (ArrayList)xmlSer.Deserialize(fs);
fs.Close();
return tempArLi;
}
}
}
Card.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace zavTarot
{
public class Card
{
#region Vars
/// <summary>
/// The name of the Card.
/// </summary>
private string name;
/// <summary>
/// The sense of the Card.
/// </summary>
private string sense;
/// <summary>
/// The upside down sense of the Card.
/// </summary>
private string upsideDownSense;
/// <summary>
/// The aspect of the Card.
/// </summary>
private string aspect;
/// <summary>
/// The principle of the Card.
/// </summary>
private string principle;
#endregion Vars
#region Props
/// <summary>
/// The name of the Card.
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// The sense of the Card.
/// </summary>
public string Sense
{
get { return sense; }
set { sense = value; }
}
/// <summary>
/// The upside down sense of the Card.
/// </summary>
public string UpsideDownSense
{
get { return upsideDownSense; }
set { upsideDownSense = value; }
}
/// <summary>
/// The aspect of the Card.
/// </summary>
public string Aspect
{
get { return aspect; }
set { aspect = value; }
}
/// <summary>
/// The principle of the Card.
/// </summary>
public string Principle
{
get { return principle; }
set { principle = value; }
}
#endregion Props
#region Constructors
public Card() { }
#endregion Constructors
}
}
lg niji