C# - 在树视图控件中将字符串解析为树 [英] C# - parsing string into tree in treeview control

查看:130
本文介绍了C# - 在树视图控件中将字符串解析为树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何以及在c#中使用特定模式将特定模式创建数据树到TreeView Control中的最佳方法是什么?最好的方法是什么?



例如我有输入像这样的字符串:



i wonder how and what is the best method to create a data tree from particular string with specific pattern into TreeView Control in c#?

For example i have input string like this :

(program (statements (statement (var_declaration (primitive_type string) (identifier a) = (expression "hi") ;)) (statement (func_declaration (predefined_fun tampilkan) ( (identifier a) ) ;))) <EOF>)





这样的数据树但是在TreeView Control中?



看看此图片

查看此图片



i需要建议&参考。



我尝试过:



试图了解树数据结构在书上一步一步。



into data tree like this but in TreeView Control?

Look at this image
Look at this image

i need an advice & reference.

What I have tried:

Tried to learn about tree data structures step by step on books.

推荐答案

这个例子使用(和) - 但这是一个微不足道的变化:c# - 树形结构为字符串 - 如何匹配嵌套大括号? - 堆栈溢出 [ ^ ]
This example uses "(" and ")" - but that's a trivial change: c# - Tree structure as string - how to match nested braces? - Stack Overflow[^]


正如谢尔盖所​​说,这里的大赢是将生成数据的格式化为字符串的任何内容转换为JSON或XML格式;将这些格式解析为Tree数据结构,或使用这些格式创建填充的TreeView控件,很简单。



如果您打算尝试使用格式的数据你在这里展示,需要让你的数据变成格式良好的形状。您当前的数据:
As Sergey suggested, the "big win" here would be to get whatever is producing your data formatted as string into JSON, or XML format; parsing those formats to a Tree data structure, or creating a populated TreeView Control using those formats, is straightforward.

If you are going to try and use data in the format you show here, need to get your data into "well formed" shape. Your current data:
<program>
	<var_declaration>
		<primitive type="" bita=""> 
		<identifier variable=""> 
		= 
		<expression 25=""> 
		;
	> 
  <eof>
></eof></expression></identifier></primitive></var_declaration></program>

显然没有系统地格式化。有很多方法可以做到这一点;例如:

Is obviously not formatted systematically. There are many ways you could do that; for example:

<program>
	<var_declaration>
		<primitive type="" bita=""> 
		<identifier variable="">
			<expression 25=""> 
		>
	> 
></expression></identifier></primitive></var_declaration></program>

将其转换为某种树结构并不困难,但是,要做到这一点,需要知道什么类型的对象(类,结构,枚举等)。 )正在使用一些通用树(不是控件)的情况下创建。如果像TreeView一样使用Control,那么你必须决定你想要用于节点的命名方案。



我写过几种类型的解析器多年来从字符串到Tree / TreeView,如果你表明你已经努力在这里实现自己的,我会很乐意进一步回应......因为,我相信,其他人也会。您也可以在网上找到示例。

Translating this to some Tree structure is not that difficult, but, to do so, one needs to know what types of objects (classes, structs, enums, etc.) are being created in the case of using some generic Tree (not a Control). If using a Control like a TreeView, then you have to decide what kind of naming scheme you want to use for the Nodes.

I've written several types of parsers from string to Tree/TreeView over the years, and if you show you have made an effort to implement your own here, I'll be happy to respond further ... as, I am sure, other people will also. You can also find examples on the net.


这篇关于C# - 在树视图控件中将字符串解析为树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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