110,534
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--C#从入门到精通课程结构-->
<知识点 id="321" name="C#从入门到精通" parentid="0" xuhao="1">
<知识点 id="322" name="HelloC#--认识C#" parentid="321"
xuhao="1">
<知识点 id="323" name="C#简介" parentid="322" xuhao="1" />
<知识点 id="324" name="C#程序运行平台——.NET框架"
parentid="322" xuhao="2" />
<知识点 id="325" name="C#程序开发利器——集成开发环境Visual
Studio 2008" parentid="322" xuhao="2" />
<知识点 id="326" name="创建第一个C#程序" parentid="322"
xuhao="4" />
<知识点 id="327" name="使用帮助" parentid="322" xuhao="5" />
</知识点>
<知识点 id="328" name="C#程序开发基础——C#语法入门"
parentid="321" xuhao="2">
<知识点 id="333" name="数据类型" parentid="328" xuhao="1" />
<知识点 id="334" name="常量与变量" parentid="328" xuhao="2" />
<知识点 id="335" name="运算符和表达式" parentid="328" xuhao="3"
/>
<知识点 id="336" name="求异存同——类型转换" parentid="328"
xuhao="4" />
<知识点 id="337" name="复杂的数据类型——数组和枚举"
parentid="328" xuhao="5" />
</知识点>
<知识点 id="329" name="C#程序开发基础——程序控制结构"
parentid="321" xuhao="3">
<知识点 id="373" name="赋值和注释语句" parentid="329"
xuhao="1">
<知识点 id="377" name="赋值语句" parentid="373" xuhao="1" />
<知识点 id="378" name="注释语句" parentid="373" xuhao="2" />
</知识点>
<知识点 id="374" name="选择正确的道路——分支结构"
parentid="329" xuhao="2">
<知识点 id="379" name="if语句" parentid="374" xuhao="1" />
<知识点 id="380" name="switch结构" parentid="374" xuhao="2" />
</知识点>
<知识点 id="375" name="循环结构" parentid="329" xuhao="3">
<知识点 id="381" name="while循环" parentid="375" xuhao="1" />
<知识点 id="382" name="for循环" parentid="375" xuhao="2" />
<知识点 id="383" name="foreach循环" parentid="375" xuhao="3" />
</知识点>
<知识点 id="376" name="字符串操作" parentid="329" xuhao="4">
<知识点 id="384" name="静态串String" parentid="376" xuhao="1"
/>
<知识点 id="385" name="动态串StringBuilder" parentid="376"
xuhao="2" />
</知识点>
</知识点>
<知识点 id="330" name="主流软件开发方法——面向对象入门"
parentid="321" xuhao="4">
<知识点 id="386" name="从特殊到一般——C#中的类"
parentid="330" xuhao="1" />
<知识点 id="387" name="从一般到特殊——C#中的对象"
parentid="330" xuhao="2" />
<知识点 id="388" name="分部类" parentid="330" xuhao="3" />
<知识点 id="389" name="结构与类" parentid="330" xuhao="4" />
<知识点 id="390" name="命名空间" parentid="330" xuhao="5" />
</知识点>
<知识点 id="331" name="深入了解面向对象——面向对象的重要特
征" parentid="321" xuhao="5">
<知识点 id="391" name="子承父业——继承" parentid="331"
xuhao="1" />
<知识点 id="392" name="蝌蚪和青蛙——多态" parentid="331"
xuhao="2" />
<知识点 id="393" name="统一的标准——接口" parentid="331"
xuhao="3" />
<知识点 id="394" name="抽象类和密封类" parentid="331" xuhao="4"
/>
<知识点 id="395" name="委托、事件和索引器" parentid="331"
xuhao="5" />
</知识点>
<知识点 id="332" name="特殊的类——集合与泛型" parentid="321"
xuhao="6">
<知识点 id="396" name="集合" parentid="332" xuhao="1" />
<知识点 id="397" name="动态数组——ArrayList类" parentid="332"
xuhao="2" />
<知识点 id="398" name="HashTable类" parentid="332" xuhao="3" />
<知识点 id="399" name="通用类型——泛型" parentid="332"
xuhao="4" />
</知识点>
</知识点>
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 操作xml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string str = "";
XmlDocument doc = new XmlDocument();
doc.Load("C#从入门到精通.xml");
DiGui(doc, str, null);
}
public void DiGui( XmlDocument doc,string str,TreeNode xtn)//此处貌似需加一个参数,我加了出不来又删了怎么办?
{
str = str + "/知识点";
XmlNodeList nodes = doc.SelectNodes(str);
foreach (XmlNode node in nodes)
{
TreeNode tn = new TreeNode();
tn.Name = node.Attributes["parentid"].Value;
tn.Text = node.Attributes["name"].Value;
if (tn.Name == "0")
{
this.treeView1.Nodes.Add(tn);
}
else
{
xtn.Nodes.Add(tn);
}
DiGui(doc, str, tn);
}
//this.treeView1.ExpandAll();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("XmlFile1.xml");
var xmlNodes = xmlDoc.SelectNodes("/知识点");
AddNodes(null, xmlNodes);
}
private void AddNodes(TreeNode parent, XmlNodeList nodes)
{
foreach (XmlNode node in nodes)
{
var name = node.Attributes["name"].Value;
var id = node.Attributes["id"].Value;
var treeNode = new TreeNode { Name = id, Text = name };
if (parent == null)
treeView1.Nodes.Add(treeNode);
else
parent.Nodes.Add(treeNode);
AddNodes(treeNode, node.ChildNodes);
}
}