改变树视图的属性值
本文关键字:属性 视图 改变 | 更新日期: 2023-09-27 18:06:49
我正在加载XML文件并将其显示为树视图。我想让用户看到什么孩子可以每个元素有。有什么办法吗?我有麻烦匹配节点(树节点与"原始"节点)。我比较了他们的名字,但我并不总是得到正确的结果。这就是我目前所看到的:
xmlFile = new XmlDocument();
xmlFile.Load(dialog.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmlFile.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
AddNode(xmlFile.DocumentElement, tNode);
treeView1.ExpandAll();
添加节点
public void AddNode(XmlNode xmlNode, TreeNode treeNodes)
{ XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (xmlNode.HasChildNodes)
{nodeList = xmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{xNode = xmlNode.ChildNodes[i];
treeNodes.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNodes.Nodes[i];
AddNode(xNode, tNode);}}
else
{ treeNodes.Text = (xmlNode.OuterXml).Trim();}}
在树节点上点击
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e){
List<string> temp = new List<string>();
this.lbElements.Items.Clear(););
foreach (XmlNode node in xmlFile.DocumentElement.ChildNodes)
{
if (node.Name == e.Node.Name)
{
foreach (string s in fh.getChildNodes(node)) temp.Add(s);
if (!temp.Contains(s))
temp.Add(s);
foreach (string s in temp) this.lbElements.Items.Add(s);
尝试使用if (node.Name == e.Node.Text && node.NodeType == XmlNodeType.Element)
最简单的方法是使用XmlReader并遍历内容。我试过了。
>
private void treeView1_NodeMouseClick_1(object sender,
> TreeNodeMouseClickEventArgs e)
> {
> XmlReader reader = null;
> reader = XmlReader.Create(filePath);
> {
> reader.MoveToContent();
> // Parse the file and display each of the nodes.
> while (reader.Read())
> {
> switch (reader.NodeType)
> {
> case XmlNodeType.Element:
> if (reader.LocalName == "to")
> {
>
> }
> break;
>
> case XmlNodeType.Text:
> if(reader.Value == "Jane")
> {
>
> }
> break;
> }
> }
> }
> }
Xml file used is:
<note>
<to>Tove</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Weekend</body>
</note>