简体   繁体   English

为什么TreeView node.text在C#中显示奇怪的结构

[英]Why TreeView node.text show strange structure in c#

I have a treeview 我有树景

Microsoft.Web.UI.WebControls.TreeView myTreeView = new Microsoft.Web.UI.WebControls.TreeView();
        myTreeView.Nodes.Clear();

And after populate data to the treeview I can show it nicely. 在将数据填充到树视图之后,我可以很好地显示它。 However now I want to get the text in each branch of that tree (from root to parents to child in deepest level), and compare, if it is a certain name I will mark it as selected. 但是,现在我想获取该树的每个分支中的文本(从根到最深的父级再到子级),并进行比较,如果它是某个名称,则将其标记为选中。

Microsoft.Web.UI.WebControls.TreeNodeCollection nodes = myTreeView.Nodes;
        foreach (Microsoft.Web.UI.WebControls.TreeNode n in nodes)
        {
            string nodetext = n.Text;
            if (nodetext == "Casting")
                myTreeView.SelectedNodeIndex = n.GetNodeIndex();
        }

However when I print nodetext , I get very confuse with its value: 但是,当我打印nodetext ,我对其值感到非常困惑:

<acronym Title='Casting, Closed Die Press Forging, Compression Moulding, Drop Forging, Injection Moulding, Moulding Compounds, Steel Atomisation, Transfer Moulding, Two Shot Moulding, Upset Forging, Vacuum Forming'>Casting, Moulding, Forming, & Forging</acronym>

While in display, it show a tree in this format: 在显示时,它以这种格式显示一棵树:

在此处输入图片说明

Why the text has that format and how could I extract exactly the each branch name as in the image? 为什么文本具有这种格式,以及如何像图像中那样准确提取每个分支名称? And is it right to turn the selected feature as I do above ? 像我上面那样打开所选功能是否正确?

I found the answer. 我找到了答案。 In other part of the code they append the nodetext for the tree by 在代码的其他部分,他们通过以下方式为树添加节点文本:

return String.Format("<acronym Title='{0}'>{1}</acronym>",

that is why it has that format. 这就是为什么它具有这种格式。 To get the text, I just have to use regex: 要获取文本,我只需要使用正则表达式:

//get text inside tag
            string regularExpressionPattern1 = @"<acronym .*?>(.*?)<\/acronym>";
            Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
            MatchCollection collection = regex.Matches(n.Text.ToString());
            Match m = collection[0];
            var stripped = m.Groups[1].Value;

Thanks for your attention! 感谢您的关注! Hope that would help some one has something related. 希望对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM