Mam w swoim projekcie taką oto klasę:
[Serializable]
public class HighScore
{
public static readonly int quantity = 5;
private string[] name = new string[quantity];
private int[] score = new int[quantity];
public HighScore()
{
for (int i = 0; i < quantity; i++)
{
name[i] = "brak";
score[i] = 0;
}
}
public HighScore(string[] nam, int[] sco)
{
System.Diagnostics.Debug.Assert(nam.Length != sco.Length
|| nam.Length != quantity, "Wrong quantity of high scores");
name = nam;
score = sco;
}
......
reszta klasy to właściwości i metody.
W innej klasie mam taki kod:
HighScore highScore = new HighScore();
highScore.AddToHighScore("michal", 300);
highScore.AddToHighScore("kuba", 100);
highScore.AddToHighScore("miszcz", 1000);
Stream streamWrite = File.Create("highscore.hs");
XmlSerializer ser = new XmlSerializer(typeof(HighScore));
ser.Serialize(streamWrite, highScore);
streamWrite.Close();
Serializacja nie wyrzuca żadnych wyjątków, kompilator nawet warningów nie zgłasza. powstaje taki oto plik:
<?xml version="1.0"?>
<HighScore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
i to tyle... żadnych danych. Czemu?
Dodam, że w tym samym assembly serializuje inną klasę i ta inna serializuje się bez najmniejszych problemów.