I tried to save and load treenodes of my treeview,I created tree with list of treenodes as follow:
我试图保存并加载我的树视图的treenodes,我创建了一个包含treenodes列表的树,如下所示:
[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;
}
}
Then In the save button I wrote this one:
然后在保存按钮中我写了这个:
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;
}
in the loaded form I used this one:
在加载的表单中我使用了这个:
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
I didn't do anything further and I think nodes.xml is not correct I didn't know what to write there if I want to create the xml file what should I do to make this works? it has invalidopeartionexception error
我没有做任何进一步的事情,我认为nodes.xml不正确我不知道在那里写什么,如果我想创建xml文件我该怎么做才能使它工作?它有invalidopeartionexception错误
1 个解决方案
#1
1
This is a simpler way of doing it the code below is more for converting any object into XML once you understand this one feel free to try the advanced code when and where needed How to convert treeview to xml?
这是一种更简单的方法,下面的代码更多的是用于将任何对象转换为XML一旦你理解了这个就可以随时随地尝试高级代码如何将treeview转换为xml?
Here is an example of how you can Serialize and Object to XML and Deserialize it I hope this example helps..
这是一个如何序列化和对象到XML并反序列化它的例子我希望这个例子有帮助..
**To write any object or some collections to xml Object must have a default constructor.
**要将任何对象或某些集合写入xml对象,必须具有默认构造函数。
public static string SerializeToXmlString(object objectToSerialize)
{
MemoryStream memoryStream = new MemoryStream();
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
xmlSerializer.Serialize(memoryStream, objectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(memoryStream.ToArray());
}
**And this should turn the xml back into an object
**这应该将xml变回一个对象
public static object DeSerializeFromXmlString(System.Type typeToDeserialize, string xmlString)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
MemoryStream memoryStream = new MemoryStream(bytes);
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(typeToDeserialize);
return xmlSerializer.Deserialize(memoryStream);
}
#1
1
This is a simpler way of doing it the code below is more for converting any object into XML once you understand this one feel free to try the advanced code when and where needed How to convert treeview to xml?
这是一种更简单的方法,下面的代码更多的是用于将任何对象转换为XML一旦你理解了这个就可以随时随地尝试高级代码如何将treeview转换为xml?
Here is an example of how you can Serialize and Object to XML and Deserialize it I hope this example helps..
这是一个如何序列化和对象到XML并反序列化它的例子我希望这个例子有帮助..
**To write any object or some collections to xml Object must have a default constructor.
**要将任何对象或某些集合写入xml对象,必须具有默认构造函数。
public static string SerializeToXmlString(object objectToSerialize)
{
MemoryStream memoryStream = new MemoryStream();
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
xmlSerializer.Serialize(memoryStream, objectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(memoryStream.ToArray());
}
**And this should turn the xml back into an object
**这应该将xml变回一个对象
public static object DeSerializeFromXmlString(System.Type typeToDeserialize, string xmlString)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
MemoryStream memoryStream = new MemoryStream(bytes);
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(typeToDeserialize);
return xmlSerializer.Deserialize(memoryStream);
}