从树节点中提取值 [英] Extracting the value from a tree node

查看:109
本文介绍了从树节点中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All:

Hello All:

我是C#的新手。我正在使用OPC Client,它产生一棵树,如下所示。我想解析树并从节点中提取值(例如,保持寄存器中的值)并分配给局部变量。任何人都可以提供任何关于如何在C#中解析树的建议

I am new to C#. I am working on OPC Client which results a tree as shown below. I want to parse the tree and extract the value from the nodes (say, for example value from holding registers) and assign in to a local variable. Can anyone provide any suggestions on how to parse a tree in C#?

谢谢,

Shwetha。

推荐答案

理想情况下你不应该这样做。 UI应该只是您存储的实际数据的表示。因此,您可以正常处理数据,然后相应地更新UI。

Ideally you shouldn't have to. The UI should just be a representation of the real data you have stored. Therefore you can simply work with your data as normal and then update the UI accordingly.

但是如果您有树视图并且想要枚举它,那么您将从根节点开始( TreeView .Nodes )然后
枚举。根据您是否要在处理每个节点时处理每个节点,或者在特定级别执行所有节点,首先确定如何继续。最终虽然它涉及查看每个节点,然后,如果它有孩子,则枚举它们。
由于我们不知道你是如何构建树的,我们不知道如何提取数据,但常见的技术是将数据存储在Tag属性中。所以你可以枚举节点并从属性中检索数据。

But if you have a treeview and you want to enumerate it then you'll start with the root nodes (TreeView.Nodes) and then enumerate. Depending upon whether you want to process each node as you get to it or do all the nodes at a particular level first determines how you proceed. Ultimately though it involves looking at each node and then, if it has children, enumerating them. Since we don't know how you built the tree we won't know how to extract the data but a common technique is to store the data in the Tag property. So you can enumerate the nodes and retrieve the data from the property.

void EnumerateTree ( TreeView tree )
{
   foreach (TreeNode node in tree.Nodes)
      EnumerateTree(node);
}

void EnumerateTree ( TreeNode node )
{
   //Process the current node
   ProcessNode(node);

   //Enumerate its children
   foreach (TreeNode child in node.Nodes)
      EnumerateTree(child);
}

void ProcessNode ( TreeNode node )
{
   //Get the data - from the Tag?
   var data = node.Tag;
}


这篇关于从树节点中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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