如何将treenode保存为xml并加载? [英] how to save treenode as xml and load?

查看:40
本文介绍了如何将treenode保存为xml并加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存和加载我的树视图的树节点,我用树节点列表创建了树,如下所示:

I tried to save and load treenodes of my treeview,I created tree with list of treenodes as follow:

  [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

我没有做任何进一步的事情,我认为nodes.xml不正确如果我想创建xml文件我不知道在那里写什么我应该怎么做才能使它起作用?它有 invalidopeartionexception 错误

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

推荐答案

这是一种更简单的方法,下面的代码更适合将任何对象转换为 XML一旦你理解了这一点,就可以随时随地尝试高级代码如何将treeview转换为xml?

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 并反序列化它我希望这个示例有帮助..

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.

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());
}

**这应该将 xml 重新变成一个对象

**And this should turn the xml back into an object

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);
}

这篇关于如何将treenode保存为xml并加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆