Problem with editing XML files in C#
Hi I am currently working on a program that allows the user to add and edit "Events" to XML files. These Events are used in a PC game and look like this:
<Event Name="some_name">
<Event_Type>TYPE</Event_Type>
<Event_Param1>parameter1</Event_Param1>
<Event_Param2>parameter2</Event_Param2>
...
<Reward_Type>TYPE</Reward_Type>
<Reward_Param1>parameter1</Reward_Param2>
...
<Prereq>Prerequisite</Prereq>
<Branch></Branch>
<Perpetual>true</Perpetual>
</Event>
Now, the program allows the user to add and edit events, which are then displayed using a treeView tool.
My problem occurs when I'm trying to overwrite an existing XML file. I can already open an existing file and all the data is displayed correctly in the treeView. Now, all the events that I'm adding to the file are saved just fine, but all the data of the events that were already existing in the file is lost, only these empty tags remain:
<Event Name="name" />
Well, here is my code:
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string save = saveFileDialog1.FileName;
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Story");
for (int i = 0; i < treeView1.Nodes.Count; i++)
{
TreeNode subnode = treeView1.Nodes[i];
XmlElement new_event = doc.CreateElement("Event");
XmlAttribute attr = doc.CreateAttribute("Name");
attr.Value = subnode.Text;
new_event.Attributes.Append(attr);
root.AppendChild(new_event);
for (int j = 0; j < subnode.Nodes.Count; j++)
{
TreeNode type_node = subnode.Nodes[j];
if (type_node.ToolTipText == "Event Type")
{
XmlElement event_type = doc.CreateElement("Event_Type");
event_type.InnerText = type_node.Text;
new_event.AppendChild(event_type);
if (type_node.Nodes.Count > 0)
{
for (int k = 0; k < type_node.Nodes.Count; k++)
{
TreeNode param_node = type_node.Nodes[k];
XmlElement param = doc.CreateElement("Event_Param" + (k + 1));
param.InnerText = param_node.Text;
new_event.AppendChild(param);
}
}
}
else if (type_node.ToolTipText == "Reward Type")
{
XmlElement reward_type = doc.CreateElement("Reward_Type");
reward_type.InnerText = type_node.Text;
new_event.AppendChild(reward_type);
if (type_node.Nodes.Count > 0)
{
for (int k = 0; k < type_node.Nodes.Count; k++)
{
TreeNode param_node = type_node.Nodes[k];
XmlElement param = doc.Create开发者_JAVA百科Element("Reward_Param" + (k + 1));
param.InnerText = param_node.Text;
new_event.AppendChild(param);
}
}
}
else if (type_node.ToolTipText == "Prereq")
{
XmlElement prereq = doc.CreateElement("Prereq");
prereq.InnerText = type_node.Text;
new_event.AppendChild(prereq);
}
else if (type_node.ToolTipText == "Branch")
{
XmlElement branch = doc.CreateElement("Branch");
branch.InnerText = type_node.Text;
new_event.AppendChild(branch);
}
else if (type_node.ToolTipText == "Perpetual")
{
XmlElement perpetual = doc.CreateElement("Perpetual");
perpetual.InnerText = type_node.Text;
new_event.AppendChild(perpetual);
}
}
}
doc.AppendChild(root);
FileStream fs = new FileStream(save, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
doc.Save(fs);
I am pretty new to C# and programming in general and even after searching google for a few hours I have no idea what could be wrong. I hope you can help me out there.
Thanks in advance
Pox
At no point do you load the original file, so you are replacing it entirely, without any of the old data. Look at .Load(path)
. Also, you haven't closed the FileStream
, and to be honest just .Save(path)
is easier.
精彩评论