XML 停止 overwriting/save 多个条目
XML stop overwriting/save multiple entries
目前我正在使用此代码创建一个 XML 文件
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
问题是它总是覆盖我之前输入的条目。我怎样才能创建多个条目并保存它们(可能在 Combobox/DropDownList 中?)。
这是我的其余代码 (btnSave btnLoad)
{
try
{
Daten daten = new Daten();
daten.GAME = txtGame.Text;
daten.PLATFORM = txtPlatform.Text;
daten.RELEASED = txtReleased.Text;
SaveData.SaveDaten(daten, "gameregister.xml");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (File.Exists("gameregister.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Daten));
FileStream read = new FileStream("gameregister.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Daten daten = (Daten)xs.Deserialize(read);
txtGame.Text = daten.GAME;
txtPlatform.Text = daten.PLATFORM;
txtReleased.Text = daten.RELEASED;
read.Close();
}
}
ps:我必须使用 XML 来完成这个特定任务。
对于您的问题,您想将多个实例序列化为XML。
您可以尝试通过以下代码获取。
Private void BtnSave_Click(object sender,EventArgs e)
{
Daten daten=new Daten();
daten.GAME=txtGame.Text;
daten.PLATFORM=txtPlatform.Text;
datem.RELEASED=txtReleased.Text;
List<Daten> lisrt=new List<Daten>();
String path=”d:\gameregister.xml”;
if(File.Exists(path))
{
StreamReader read=new StreamReader(path);
XmlSerializer xc=new XmlSerializer(typeof(List<Daten>));
list=(List<Daten>)xc.Deserialize(read);
read.Close();
Textwriter writer=new StreamWriter(path);
list.Add(daten);
xc.Serialize(writer,list);
writer.Close();
MessageBox.Show(“Serialization succeeded again!;”);
}
else
{
list.Add(daten);
XmlSerializer xs=new XmlSerializer(typeof(List<Daten>));
Textwriter writer=new StreamWriter(path);
xs.Serialize(writer,list);
writer.Close();
MessageBox.Show(“Serialization successful!”);
}
}
目前我正在使用此代码创建一个 XML 文件
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
问题是它总是覆盖我之前输入的条目。我怎样才能创建多个条目并保存它们(可能在 Combobox/DropDownList 中?)。
这是我的其余代码 (btnSave btnLoad)
{
try
{
Daten daten = new Daten();
daten.GAME = txtGame.Text;
daten.PLATFORM = txtPlatform.Text;
daten.RELEASED = txtReleased.Text;
SaveData.SaveDaten(daten, "gameregister.xml");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (File.Exists("gameregister.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Daten));
FileStream read = new FileStream("gameregister.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Daten daten = (Daten)xs.Deserialize(read);
txtGame.Text = daten.GAME;
txtPlatform.Text = daten.PLATFORM;
txtReleased.Text = daten.RELEASED;
read.Close();
}
}
ps:我必须使用 XML 来完成这个特定任务。
对于您的问题,您想将多个实例序列化为XML。
您可以尝试通过以下代码获取。
Private void BtnSave_Click(object sender,EventArgs e)
{
Daten daten=new Daten();
daten.GAME=txtGame.Text;
daten.PLATFORM=txtPlatform.Text;
datem.RELEASED=txtReleased.Text;
List<Daten> lisrt=new List<Daten>();
String path=”d:\gameregister.xml”;
if(File.Exists(path))
{
StreamReader read=new StreamReader(path);
XmlSerializer xc=new XmlSerializer(typeof(List<Daten>));
list=(List<Daten>)xc.Deserialize(read);
read.Close();
Textwriter writer=new StreamWriter(path);
list.Add(daten);
xc.Serialize(writer,list);
writer.Close();
MessageBox.Show(“Serialization succeeded again!;”);
}
else
{
list.Add(daten);
XmlSerializer xs=new XmlSerializer(typeof(List<Daten>));
Textwriter writer=new StreamWriter(path);
xs.Serialize(writer,list);
writer.Close();
MessageBox.Show(“Serialization successful!”);
}
}