C#遍历XML文件动态加载菜单
通过遍历XML文件动态加载菜单,顺便利用WebBrowser控件实现一个简单的桌面浏览器
效果如下:

代码如下:
XMLFile1.xml
<?xml version="1.0" standalone="yes"?>
<ds1>
<dtbl1>
<Name>搜狐播客</Name>
<URl>http://blog.sohu.com/</URl>
</dtbl1>
<dtbl1>
<Name>网易博客</Name>
<URl>http://blog.163.com/</URl>
</dtbl1>
<dtbl1>
<Name>新浪博客</Name>
<URl>http://weibo.com</URl>
</dtbl1>
<!--C#正则表达式-->
<dtbl2>
<Name>淘宝</Name>
<URL>http://www.taobao.com</URL>
</dtbl2>
<dtbl2>
<Name>一号店</Name>
<URL>http://www.yhd.com</URL>
</dtbl2>
<dtbl2>
<Name>京东</Name>
<URL>http://www.jd.com</URL>
</dtbl2>
</ds1>
From1.Designer.cs
namespace Dynamic_loading_menu
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.menus1 = new System.Windows.Forms.ToolStripMenuItem();
this.menus2 = new System.Windows.Forms.ToolStripMenuItem();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menus1,
this.menus2});
this.menuStrip1.Location = new System.Drawing.Point(, );
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(, );
this.menuStrip1.TabIndex = ;
this.menuStrip1.Text = "menuStrip1";
//
// menus1
//
this.menus1.Image = global::Dynamic_loading_menu.Properties.Resources.folder;
this.menus1.Name = "menus1";
this.menus1.Size = new System.Drawing.Size(, );
this.menus1.Text = "博客";
//
// menus2
//
this.menus2.Image = global::Dynamic_loading_menu.Properties.Resources.folder;
this.menus2.Name = "menus2";
this.menus2.Size = new System.Drawing.Size(, );
this.menus2.Text = "电商";
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(, );
this.webBrowser1.MinimumSize = new System.Drawing.Size(, );
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(, );
this.webBrowser1.TabIndex = ;
this.webBrowser1.Url = new System.Uri("http://www.baidu.com", System.UriKind.Absolute);
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
this.webBrowser1.NewWindow += new System.ComponentModel.CancelEventHandler(this.webBrowser1_NewWindow);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem menus1;
private System.Windows.Forms.ToolStripMenuItem menus2;
private System.Windows.Forms.WebBrowser webBrowser1;
}
}
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; namespace Dynamic_loading_menu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
#region 动态加载菜单 DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath + "\\XMLFile1.xml"); foreach (DataRow drow in ds.Tables[].Rows)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = drow[].ToString();
item.Tag = drow[];
item.Click += new EventHandler(item_Click);
menus1.DropDownItems.Add(item);
}
foreach (DataRow drow in ds.Tables[].Rows)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = drow[].ToString();
item.Tag = drow[];
item.Click += new EventHandler(item_Click);
menus2.DropDownItems.Add(item);
}
#endregion
}
private void item_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender; this.webBrowser1.Navigate(item.Tag.ToString()); }
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//将所有的链接的目标,指向本窗体
foreach (HtmlElement archor in this.webBrowser1.Document.Links)
{
archor.SetAttribute("target", "_self");
}
//将所有的FORM的提交目标,指向本窗体
foreach (HtmlElement form in this.webBrowser1.Document.Forms)
{
form.SetAttribute("target", "_self");
}
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
} }
}
C#遍历XML文件动态加载菜单的更多相关文章
- android sax解析xml 文件 动态加载标题
要解决一个问题 : 问题描述为 把标题动态的加载到 listView子布局中 我们首先通过 java程序写一个把标题写到xml文件的程序.这个程序会在以后讲解. 现在截图 已经写好的xm文件格式如下 ...
- Vue + Element UI 实现权限管理系统 前端篇(十):动态加载菜单
动态加载菜单 之前我们的导航树都是写死在页面里的,而实际应用中是需要从后台服务器获取菜单数据之后动态生成的. 我们在这里就用上一篇准备好的数据格式Mock出模拟数据,然后动态生成我们的导航菜单. 接口 ...
- Vue + Element UI 实现权限管理系统(动态加载菜单)
动态加载菜单 之前我们的导航树都是写死在页面里的,而实际应用中是需要从后台服务器获取菜单数据之后动态生成的. 我们在这里就用上一篇准备好的数据格式Mock出模拟数据,然后动态生成我们的导航菜单. 接口 ...
- 在mvc中动态加载菜单
最近做了一个项目, 要在客户端动态的显示菜单,也就是这些菜单是保存在数据库中的, 在客户端动态加载菜单,这样做的好处很明显,就是菜单很容易修改,直接在后台进行维护,再也不会直接在前面的 视图页面中进行 ...
- MP实战系列(十八)之XML文件热加载
你还在为每次修改XML文件中的SQL重新启动服务器或者是等待几分钟而烦恼吗? 配置了热加载即可解决你的这个问题. 这就是XML文件热加载的目的,减少等待时间成本,提高开发效率. SSM框架配置(Spr ...
- Excel催化剂开源第7波-VSTO开发中Ribbon动态加载菜单
在VS开发环境中,特别是VSTO的开发,微软已经现成地给开发者准备了设计器模式的功能区开发,相对传统的VBA.ExcelDna和其他方式的COM加载项开发来说,不需要手写xml功能区,直接类似拖拉窗体 ...
- Spring Framework框架解析(1)- 从图书馆示例来看xml文件的加载过程
引言 这个系列是我阅读Spring源码后的一个总结,会从Spring Framework框架的整体结构进行分析,不会先入为主的讲解IOC或者AOP的原理,如果读者有使用Spring的经验再好不过.鉴于 ...
- DirectUI界面编程(三)从XML文件中加载界面
Duilib支持xml界面布局,使得界面设计与逻辑处理相分离,本节介绍如何从xml文件中加载界面元素. 我们需要以下几个步骤: 创建并初始化CPaintManagerUI对象. 创建CDialogBu ...
- 将Xml文件递归加载到TreeView中
#region [通过XDocument的方式将Xml文件递归到TreeView控件中] //读取Xml文件(XDocument) //1.加载Xml文件 XDocument document=XD ...
随机推荐
- mac 下jetbrains IDE系列IDE主题
1.直接粘贴导入 使用shift+command+g键进入: ~/Library/Preferences/ 在下边找到当前的IED(WebStrom.IdealIC.PyCharm) 然后在下边找到c ...
- OSChina码云试用
首先在码云申请账户. 从 http://git-scm.com/download 下载window版的客户端.下载好,一步一步安装即可. $git config --global user.name ...
- J2EE版本
Different versions of JEE: Note: JPE (Java Professional Edition) project announced in May 1998 at Su ...
- Asp.Net_Mvc_获取当前Url、Controller、Action
一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...
- Android异步任务机制之AsycTask
在Android中实现异步任务机制有两种方式,Handler和AsyncTask. 本篇就说说AsyncTask的异步实现. 1.什么时候使用 AsnyncTask 在上一篇文章已经说了,主线程主要负 ...
- 深入理解javascript系列,读书笔记
深入理解JavaScript系列(2):揭秘命名函数表达式 1.讲了函数声明和函数表达式的区别,包括一些在函数提升上的区别 2.如果给函数表达式的函数也取名,会在调试的时候受益 3.不要在block( ...
- Hibernate连数据库
1.建数据库,建表(一定要设主码) create database Hibernate create table Students( sno char(10) primary key, sna cha ...
- 移动web点5像素的秘密
最近和一个朋友聊天,朋友吐露了工作上的一些不开心,说自己总是喜欢跟别人比较,活得比较累,这种感觉大部分人经历过,往往觉得是自己心态不好,其实不然,这是人性,此时应该快速摆脱这种状态,想到DOTA大9神 ...
- 关于absolute 和 relative 定位的定义
absolute的英文意思是绝对的意思,实际上是针对父级元素元素定位,如果父级元素没有position:relative|absolute,则追至再上一个父级元素,直至相对于文档的左上角定位,按照我们 ...
- diskpart查看硬盘序列号
WIN + R键运行cmd,进如DOS界面: 1. systeminfo查看OS初始安装时间 2. diskpart工具查看硬盘序列号 (1)diskpart (2)lisk disk 查看主机安装的 ...