我试图保存和加载我的树视图的树节点,我用树节点列表创建了树,如下所示:
[Serializable]
public class Tree : List<TreeNode>
{
public void Save()
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Tree));
System.IO.FileStream s = new System.IO.FileStream(Application.StartupPath + "\\nodes.xml", System.IO.FileMode.Create);
x.Serialize(s, this);
s.Flush();
s.Close();
}
public static Tree Load()
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Tree));
System.IO.FileStream s = new System.IO.FileStream(Application.StartupPath + "\\nodes.xml", System.IO.FileMode.OpenOrCreate);
Tree tree = x.Deserialize(s) as Tree;
s.Close();
return tree;
}
}
然后在保存按钮中我写了这个:
private void SaveButton_Click(object sender, EventArgs e)
{
this.SaveButton.Enabled = false;
Tree tree = new Tree();
foreach (TreeNode treeNode in this.treeView1.Nodes)
{
tree.Add(treeNode);
}
tree.Save();
MessageBox.Show("Saved Successfully.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.SaveButton.Enabled = true;
}
在加载的表格中,我使用了这个:
private void Form1_Load(object sender, EventArgs e)
{
Tree tree = Tree.Load();
//Process Tree
foreach (TreeNode node in tree)
{
TreeNode treeNode=new TreeNode(node.Text);
this.treeView1.Nodes.Add(node);
}
//End Process Tree
我没有做任何进一步的事情,我认为nodes.xml不正确如果我想创建xml文件我不知道在那里写什么我应该怎么做才能使它工作?它有无效操作异常错误