xml 形成树形列表的的问题

xwrj 2011-11-01 11:37:00
有个xml文件 如下
<?xml version="1.0" encoding="utf-8" ?>

<neirong>
<kehu text="内一科" bz="门诊">
<ldc>李四</ldc>
<ldc>张三</ldc>
</kehu>
<kehu text="内二科" bz="门诊住院">
<ldc>王五</ldc>
<ldc>钱柜</ldc>
</kehu>
</neirong>

我要读取他的内容形成一个树形列表

内一科 门诊
李四
张三
内二科 门诊住院
王五
钱柜

如何书写代码 我抄了下面的代码成了这样
neirong
kehu
ldc
李四
ldc
张三
kehu
ldc
王五
ldc
钱柜

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.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
private void button3_Click(object sender, EventArgs e)
{


try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(@"D:\neirong.xml");

// 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);
treeView1.ExpandAll();

}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

...全文
67 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧