如何在C#中序列化树视图控件 [英] How to serialize the tree view control in C#

查看:147
本文介绍了如何在C#中序列化树视图控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace TreeViewControl
{
    [Serializable]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string n = textBox1.Text;
            if (treeView1.SelectedNode == null)
            {
                treeView1.Nodes.Add(n, n);
            }
            else
            {
                treeView1.SelectedNode.Nodes.Add(n, n);
            }
        }
    }
  }



//有人请帮助如何序列化给定的树视图控件



我尝试过:



i试图导出到xml文件,所有其他我的级别方法


//somebody pls help how to serialize the given tree view control

What I have tried:

i have tried to export to xml file and all other my level approaches

推荐答案

只需在其上添加[Serializable]属性即可序列化表单和控件。



您必须编写代码以从控件中获取所需数据并自行序列化该数据。谷歌为序列化树视图C#提供了大量示例。
Forms and controls are not serializable by just putting the [Serializable] attribute on them.

You have to write the code to grab the data you want from the controls and serialize that data yourself. Google for "Serialize treeview C#" for plenty of examples.


正如MSDN所述:

As MSDN states:
Quote:

序列化是将对象转换为字节流以便存储对象或将其传输到内存,数据库或文件的过程。其主要目的是保存对象的状态,以便能够在需要时重新创建它。反向过程称为反序列化。

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

source:序列化(C#) [ ^ ]



这意味着您只能序列化对象(数据),而不能序列化控件!



根据你想要达到的目的,你可能需要阅读:

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

自定义类集合序列化和反序列化的完整示例 [ ^ ]

将父子层次结构序列化为XML [ ^ ]

source: Serialization (C# )[^]

This means that you can serialize only objects (data), but not controls!

Depending on what you want to achieve, you may need to read this:
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
Serialize a parent-children hierarchy to XML[^]


Google [< a href =https://www.google.com/#q=c%23+serialise+control+to+xmltarget =_ blanktitle =New Window> ^ ]



如果您对特定方法有特定问题,请相应地更新您的问题。
Google[^]

If you have a specific issue with a specific approach then update your question accordingly.


这篇关于如何在C#中序列化树视图控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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