我搜索了很多但仍然无法正常工作,所有我的int和Strings等都不会改变他们的标签
我的解/序列化代码:
public void serialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Stundenplan));
TextWriter writer = new StreamWriter(@"daten.xml");
xs.Serialize(writer, this);
writer.Close();
} //seems to work since the xml file looks good
public void deserialization()
{
XmlSerializer xs = new XmlSerializer(typeof(Stundenplan));
TextReader reader = new StreamReader(@"daten.xml");
Stundenplan stu;
stu = (Stundenplan)xs.Deserialize(reader);
} // don't see any change
以下是我参与序列化的类,没有所有的空白:
public class Stundenplan
{
public Fach[] faecher = new Fach[100];
public int fachnr = 0;
.......(很多空洞) }
public class Fach
{
public String name;
public bool[] stunden = new bool[8];
public Hausaufgabe[] hausaufgaben = new Hausaufgabe[10000];
public int hausaufgabennr = 0;
public String lehrer;
....(也有几个空洞) }
public class Hausaufgabe
{
public DateTime datum;
public String beschreibung;
public bool gemacht;
public Hausaufgabe()
{
}
}
缺少什么/错误?
答案 0 :(得分:6)
问题出在以下几行:
stu = (Stundenplan)xs.Deserialize(reader);
您要将值反序列化为另一个实例(stu
而不是this
),并在方法结束时将其丢弃。您应该像这样重写deserialization()
方法:
public static Stundenplan deserialization()
{
XmlSerializer xs = new XmlSerializer(typeof(Stundenplan));
TextReader reader = new StreamReader(@"daten.xml");
return (Stundenplan)xs.Deserialize(reader);
}
当然你也应该改变你的召唤方式:
Stundenplan sp = Stundenplan.deserialization();
而不是
Stundenplan sp = new Stundenplan();
sp.deserialization();