ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:141.42KB ,
资源ID:3405915      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3405915.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(文件递归XML递归树图递归.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

文件递归XML递归树图递归.docx

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