只想显示红框里面的内容。
private void toolStripButton1_Click(object sender, EventArgs e) { //从XML中读取数据到TreeView if (this.dlgOpen.ShowDialog() == DialogResult.OK) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(dlgOpen.FileName); XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes; this.treeView1.BeginUpdate(); this.treeView1.Nodes.Clear(); XmlNode2TreeNode(xmlNodes, this.treeView1.Nodes); this.treeView1.EndUpdate(); } } private void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode) { foreach (XmlNode var in xmlNode) { if (var.NodeType != XmlNodeType.Element) { continue; } TreeNode newTreeNode = new TreeNode(); newTreeNode.Text = var.Attributes["Name"] != null ? var.Attributes["Name"].Value : null; if (var.HasChildNodes) { if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA) { newTreeNode.Tag = var.ChildNodes[0].Value; } XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes); } treeNode.Add(newTreeNode); } }
解决方案
40
XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;
改成:
XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes;
改成:
XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes;