递归,将带有属性的XML文件解析为TreeView c#我目前正在进行一个项目,在这个项目中,应用程序将接收XML文件,并将其显示在C#中的TreeView中。我正在使用VisualStudio 10编写这段代码。我不能限制显示属性的次数。我使用一个foreach循环遍历它所拥有的每个属性并显示它,但是它只对它在节点下的每个子节点显示一次属性。如何修改此代码,使其只显示属性一次?using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Xml;namespace xmlToTreeview{
public partial class Form1 : Form
{
string samplePath = Application.StartupPath + @"\\sample.xml";
public Form1()
{
InitializeComponent();
DisplayTreeView(samplePath);
}
private void DisplayTreeView(string pathname)
{
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(pathname);
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
2 回答
- 2 回答
- 0 关注
- 734 浏览
添加回答
举报
0/150
提交
取消