通过遍历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. Java集合常用类特点整理

    集合的结构如下图所示: 集合的两个顶级接口分别为:Collection和Map Collection下有两个比较常用的接口分别是List(列表)和Set(集),其中List可以存储重复元素,元素是有序 ...

  2. 1012. The Best Rank (25)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  3. ajax提交表单+前端验证小示例

    <script src="http://css.jj.cn/js/jquery.js" type="text/javascript"></sc ...

  4. Struts2中Action的使用(Struts2_Action)

    一.Action概要 二.动态调用   三.通配符(规则:*_*等价于{1_2}) 四.获取参数 1 1.方式一:逐个获取(推荐使用) 2 package com.aaron.action.param ...

  5. Software Engineering: 3. Project planning

    recourse: "Software Engineering", Ian Sommerville Keywords for this chapter: planning sche ...

  6. VB调用sendinput API

    http://files.cnblogs.com/files/liuzhaoyzz/VB%E8%B0%83%E7%94%A8sendinput_API.rar sendinput只支持发送字符或者组合 ...

  7. Sql获取周、月、年的首尾时间。

    ,) -- 本周周一 ,,,)) -- 本周周末 ,) -- 本月月初 ,,,)) -- 本月月末 ,,) -- 上月月初 ,,)) -- 上月月末 ,) -- 本年年初 ,,,)) -- 本年年末 ...

  8. windows socket

    //服务器 // winsvr.cpp : Defines the entry point for the console application. // #include "stdafx. ...

  9. 《精通C#》十四章-.NET程序集入门

    在书中,这一章节的开头说的是自定义命名空间和使用命名空间,在以我目前有限的经验来说,程序集就是一个类库经过编译之后,所生成的一个在引用命名空间,进而使用该文件中已经定义好的字段,属性以及方法的文件,以 ...

  10. c# 打乱数组

    有时候得到了一个List,我想把它随机排列一下顺序.而且如果针对不同类型的List都能用,就要用到泛型. 其实思想很简单,就是从原List中每次随机取一项,添加到新的List中,并在原List中删除. ...