通过遍历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;
}
}
 
From1.cs
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文件动态加载菜单的更多相关文章

  1. android sax解析xml 文件 动态加载标题

    要解决一个问题 : 问题描述为 把标题动态的加载到 listView子布局中 我们首先通过 java程序写一个把标题写到xml文件的程序.这个程序会在以后讲解. 现在截图 已经写好的xm文件格式如下 ...

  2. Vue + Element UI 实现权限管理系统 前端篇(十):动态加载菜单

    动态加载菜单 之前我们的导航树都是写死在页面里的,而实际应用中是需要从后台服务器获取菜单数据之后动态生成的. 我们在这里就用上一篇准备好的数据格式Mock出模拟数据,然后动态生成我们的导航菜单. 接口 ...

  3. Vue + Element UI 实现权限管理系统(动态加载菜单)

    动态加载菜单 之前我们的导航树都是写死在页面里的,而实际应用中是需要从后台服务器获取菜单数据之后动态生成的. 我们在这里就用上一篇准备好的数据格式Mock出模拟数据,然后动态生成我们的导航菜单. 接口 ...

  4. 在mvc中动态加载菜单

    最近做了一个项目, 要在客户端动态的显示菜单,也就是这些菜单是保存在数据库中的, 在客户端动态加载菜单,这样做的好处很明显,就是菜单很容易修改,直接在后台进行维护,再也不会直接在前面的 视图页面中进行 ...

  5. MP实战系列(十八)之XML文件热加载

    你还在为每次修改XML文件中的SQL重新启动服务器或者是等待几分钟而烦恼吗? 配置了热加载即可解决你的这个问题. 这就是XML文件热加载的目的,减少等待时间成本,提高开发效率. SSM框架配置(Spr ...

  6. Excel催化剂开源第7波-VSTO开发中Ribbon动态加载菜单

    在VS开发环境中,特别是VSTO的开发,微软已经现成地给开发者准备了设计器模式的功能区开发,相对传统的VBA.ExcelDna和其他方式的COM加载项开发来说,不需要手写xml功能区,直接类似拖拉窗体 ...

  7. Spring Framework框架解析(1)- 从图书馆示例来看xml文件的加载过程

    引言 这个系列是我阅读Spring源码后的一个总结,会从Spring Framework框架的整体结构进行分析,不会先入为主的讲解IOC或者AOP的原理,如果读者有使用Spring的经验再好不过.鉴于 ...

  8. DirectUI界面编程(三)从XML文件中加载界面

    Duilib支持xml界面布局,使得界面设计与逻辑处理相分离,本节介绍如何从xml文件中加载界面元素. 我们需要以下几个步骤: 创建并初始化CPaintManagerUI对象. 创建CDialogBu ...

  9. 将Xml文件递归加载到TreeView中

    #region [通过XDocument的方式将Xml文件递归到TreeView控件中] //读取Xml文件(XDocument) //1.加载Xml文件 XDocument  document=XD ...

随机推荐

  1. 使用VC++ ATL实现iStylePDF的COM插件

    本文介绍了一种使用VC++ ATL(Active Template Library),利用ISPExtensibility接口,为 iStylePDF 加入功能简单的COM插件(addin),加入工具 ...

  2. Rdlc报表 数据汇总分组展示

    1.从工具箱拉出表或者矩阵(本次使用的是矩阵) 2.选择需要的数据集,没有就新建一个数据集,名称自己起好,下面有用到 3.将行组和行列显示出来(右击报表--试图=>) 4.双击行组下的RowGr ...

  3. 上传文件fileupload

    文件上传: 需要使用控件-fileupload 1.如何判断是否选中文件? FileUpload.FileName -  选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.va ...

  4. Exit code from container executor initialization is : 24 ExitCodeException exitCode=24: Configuration file ../etc/hadoop/container-executor.cfg not found.

    /etc/hadoop/conf的软链接(/etc/hdfs/conf)不正确,应该是/etc/yarn/conf

  5. 小程序 web 端实时运行工具

    微信小程序 web 端实时运行工具 https://chemzqm.github.io/wept/

  6. centos6.5下安装qq2012

    大家如果想在linux下用QQ的话,最好的方法就是用wine来安装.腾讯的QQ for linux用起来很坑爹. 下面就是我用wine在centOS下安装QQ.中间过程很简单. 一.安装Wine. 1 ...

  7. Linux文件权限查看及修改命令chmod

    查看权限 Linux文件访问权限分为可读,可写和可执行三种. 可用ls -l命令查看,例: ls -l或者 ll 显示为 -r--r--r--. 1 root root 21 Jan 5 23:02 ...

  8. Unity脚本时间执行顺序

    1.Awake Awake用于脚本唤醒.此方法为系统执行的第一个方法,用于脚本的初始化,在脚本的生命周期中只执行一次. 2.Start Start方法在Awake之后执行,在脚本生命周期中只执行一次. ...

  9. window内容

    window parent top location.href location.reload location.replace

  10. IdentityServer4 简单使用,包括api访问控制,openid的授权登录,js访问

    写在前面 先分享一首数摇:http://music.163.com/m/song?id=36089751&userid=52749763 其次是:对于identityServer理解并不是特别 ...