1、文件递归XML递归树图递归常见的三种递归介绍:FileSystem、XML、TreeView 2一、文件系统递归 2详细代码如下: 2二、树图递归数据表多层关系 5详细代码如下: 6三、XML文件递归 9详细代码如下: 11常见的三种递归介绍:FileSystem、XML、TreeView面试中经常遇到的三种递归:文件系统递归、XML递归、树图TreeView递归。以下代码开发语言:C#,属于Windows简单应用程序。希望对初进入职场的求职者有所帮助。一、文件系统递归在Visual Studio 2005中 新建项目 Windows应用程序:命名为RecursionDemo/在默认的Form
2、1窗体加入三个控件Button:btnSearch,TreeView:tvFiles,ImageList:imageList1/设置tvFiles.ImageList = imageList1;/为imageList1添加3个图片详细代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;name
3、space RecursionDemo public partial class Form1 : Form public Form1() InitializeComponent(); / / 查询按钮的Click事件绑定TreeView / / / private void btnSearch_Click(object sender, EventArgs e) tvFiles.Nodes.Clear();/清空节点 /用户选择文件夹获得选择的文件夹的路径 FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialo
4、g() = DialogResult.OK) TreeNode rootNode = new TreeNode(); rootNode.Text = Path.GetFileName(fbd.SelectedPath); rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 1; tvFiles.Nodes.Add(rootNode); /SearchFiles(rootNode.Nodes, fbd.SelectedPath); SearchFilesMutation(rootNode, fbd.SelectedPath);/用上面一行
5、代/码也可以 tvFiles.SelectedNode = rootNode; else MessageBox.Show(没有选择文件夹或目录!, 提示); / / 查询文件和所有文件夹(目录) / 思路:.获得目录path下的所有子文件夹(子目录)、获得目录path下的所有文/件 / 2.如果是子目录,添加该子目录路径到TreeNodeCollection,继续递归子目录; / 3.如果是文件,则只需添加文件路径到TreeNodeCollection / / 树节点集合 / 当前文件夹路径 private void SearchFiles(TreeNodeCollection nodes,
6、string path) string folders = Directory.GetDirectories(path);/获取path文件夹下/所有的子文件夹 foreach (string folderPath in folders) TreeNode tempNode = new TreeNode(); tempNode.Text = Path.GetFileName(folderPath); int currentIndex = nodes.Add(tempNode); nodescurrentIndex.ImageIndex = 0; /树节点未选中时的图/片 nodescurren
7、tIndex.SelectedImageIndex = 1;/树节点处于选中时的/图片 /如果tempNode存在子目录继续遍历 SearchFiles(tempNode.Nodes, folderPath); /这里递归 string files = Directory.GetFiles(path);/获取path文件夹下所有的文/件 foreach (string filePath in files) TreeNode node = nodes.Add(Path.GetFileName(filePath); node.ImageIndex = 2; node.SelectedImageIn
8、dex = 2; / / 查询文件和文件夹的另一种方法,和SearchFiles方法原理一致 / / 当前树节点 / 当前树节点(目录)的路径 private void SearchFilesMutation(TreeNode node, string path) /获取path下的所有文件和目录组成一个string数组数组元素为物理路径 string fileAndFolders = Directory.GetFileSystemEntries(path); foreach (string fileOrFolderPath in fileAndFolders) if (Directory.E
9、xists(fileOrFolderPath) /如果是目录(文件夹) TreeNode tempNode = new TreeNode(); tempNode.Text = Path.GetFileName(fileOrFolderPath); int currentIndex = node.Nodes.Add(tempNode); node.NodescurrentIndex.ImageIndex = 0; /树节点未选/中时的图片 node.NodescurrentIndex.SelectedImageIndex = 1;/树节点处于/选中时的图片 /如果tempNode存在子目录继续遍
10、历 SearchFilesMutation(tempNode, fileOrFolderPath); /这里递归 else /如果是文件即:File.Exists(fileOrFolderPath) TreeNode currentNode = node.Nodes.Add(Path.GetFileName(fileOrFolderPath); currentNode.ImageIndex = 2; currentNode.SelectedImageIndex = 2; 运行程序。点击按钮,选择一个文件夹,效果如下图(文件夹选中和未选中时的图标不同):二、树图递归数据表多层关系以Sqlserv
11、er数据库的一张部门表Department为例,查询显示所有的部门,并按从属关系绑定到树图上。在SqlServer中,新建部门表Department,sql语句如下:CREATE TABLE Department ( DepartmentNo varchar (12) COLLATE Chinese_PRC_CI_AS NOT NULL , ParentNo varchar (12) COLLATE Chinese_PRC_CI_AS NULL , DepartmentName nvarchar (30) COLLATE Chinese_PRC_CI_AS NOT NULL , Remark
12、nvarchar (200) COLLATE Chinese_PRC_CI_AS NULL , CONSTRAINT PK_Department PRIMARY KEY CLUSTERED ( DepartmentNo ) ON PRIMARY ) ON PRIMARY-当ParentNo为null时,即没有父部门(上一级部门),我们认为是顶级部门。添加数据到Department。如下:在项目RecursionDemo下,添加 Windows窗体Form2,在Form2上添加两个控件Button:btnExamineDepartment,TreeView:tvDepartment详细代码如下:
13、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace RecursionDemo public partial class Form2 : Form public Form2() InitializeComponent(); / / 查看所有部门并绑定到树图上
14、/ / / private void btnExamineDepartment_Click(object sender, EventArgs e) tvDepartment.Nodes.Clear(); try DataTable dtTopDepartment = GetTopDepartemnt();/获得最顶级部门 /调用递归方法 BindTreeView(tvDepartment.Nodes, dtTopDepartment); catch (Exception ex) MessageBox.Show(ex.Message, 异常信息); / / 绑定树图用到的递归方法 / 思路:查看
15、Datatable的所有部门,绑定到树图上,形成多个TreeNode / 2.将生成的每一个TreeNode的Text属性设置为DepartmentName,将TreeNode的/ Tag属性设置为DepartmentNo / 3.查看每一个TreeNode,看是否存在子部门(DataTable的数据行大于,父节点/ 就是当前的Tag) / 4.如果存在子部门继续递归遍历 / / 节点集合 / 当前DataTable private void BindTreeView(TreeNodeCollection nodes, DataTable dt) foreach (DataRow dr in
16、dt.Rows) TreeNode tempNode = new TreeNode(); tempNode.Text = drDepartmentName.ToString(); tempNode.Tag = drDepartmentNo.ToString(); nodes.Add(tempNode); DataTable tempTable = GetSubDepartment(drDepartmentNo.ToString(); /上一行代码也可写成: /DataTable tempTable = /GetSubDepartment(tempNode.Tag.ToString(); if
17、(tempTable.Rows.Count 0) /如果存在子部门 BindTreeView(tempNode.Nodes, tempTable);/这里进行递归寻找/子部门 / / 获得下一级直属子部门 / / 父部门编号 / private DataTable GetSubDepartment(string parentNo) SqlConnection conn = new SqlConnection(); /根据实际情况写出自己的SqlServer数据库连接字符串 conn.ConnectionString = Data Source=LENOVO-YANGLQYLQTEST;init
18、ial catalog=ICChannels;user id=sa;password=123456; string sql = select DepartmentNo,DepartmentName from Department where ParentNo=ParentNo; SqlParameter parameter = new SqlParameter(ParentNo, SqlDbType.VarChar, 12); parameter.Value = parentNo; SqlCommand command = new SqlCommand(sql, conn); command.
19、Parameters.Add(parameter); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataSet ds = new DataSet(); conn.Open(); adapter.Fill(ds); conn.Close(); return ds.Tables0; / / 获得最顶级部门,即没有父节点(ParentNo is null) / / private DataTable GetTopDepartemnt() SqlConnection conn = ne
20、w SqlConnection(); conn.ConnectionString = Data Source=LENOVO-YANGLQYLQTEST;initial catalog=ICChannels;user id=sa;password=123456; string sql = select DepartmentNo,DepartmentName from Department where ParentNo is null; SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(
21、); conn.Open(); adapter.Fill(ds); conn.Close(); return ds.Tables0; 点击按钮,运行效果如下图:三、XML文件递归查看一个XML文件,并按父节点与子节点的归属关系 绑定到树图上。(XML文件有且只有一个根节点)一个xml文件(命名为test.xml)的内容如下: 流水号 企业编码 认证码 时间戳 用户开户 流水号1 开户卡号1 手机号1 工单处理结果:1 操作成功 流水号2 开户卡号2 手机号2 工单处理结果:99 操作失败 流水号3 开户卡号3 手机号3 工单处理结果:0 操作失败 字段1 卡号 字段2 古剑奇谭 XML解析响应
22、码 响应码在项目RecursionDemo下,添加 Windows窗体Form3,在Form3上添加两个控件Button:btnExamineXml,TreeView:tvXml详细代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;namespace RecursionDemo publi
23、c partial class Form3 : Form public Form3() InitializeComponent(); private void btnExamineXml_Click(object sender, EventArgs e) tvXml.Nodes.Clear(); XmlDocument document = new XmlDocument(); /填写自己所建的test.xml的路径 string fileName = D:test.xml; try document.Load(fileName); catch (Exception ex) MessageBo
24、x.Show(ex.Message, 异常信息); return; /XmlElement继承于类XmlNode TreeNode rootNode = new TreeNode(); rootNode.Text = document.DocumentElement.Name; tvXml.Nodes.Add(rootNode); BindXml(rootNode.Nodes, document.DocumentElement); / / 递归方法 / 思路: 1.查看node下的所有子节点 / 2.将子节点绑定到TreeNode上,如果子节点存在属性,将属性页显示出来 / 3.如果subNo
25、de存在子节点继续递归遍历 / / 树节点集合 / XML的一个节点 private void BindXml(TreeNodeCollection nodes, XmlNode node) foreach (XmlNode subNode in node.ChildNodes) string text = ; if (subNode.Value = null) /如果节点值不存在 if (subNode.Attributes != null & subNode.Attributes.Count 0) text += subNode.Name + -属性有:; for (int i = 0; i subNode.Attributes.Count; i+) text += subNode.Attributesi.Name + : + subNode.Attributesi.Value + ; else
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1