加载XML文件到系统中
using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Collections.Generic;
namespace XMLFileLoad.Common
{
public class StepRuleSetting
{
private const string SettingsPath = "StepRule.xml";
private static StepRuleSetting _instance;
private readonly DataTable StepRuleDT = new DataTable();
private readonly Dictionary<string,List<string>> DicRules = new Dictionary<string,List<string>>();
private List<string> Rules = new List<string>();
private StepRuleSetting()
{
}
public static StepRuleSetting Instance
{
get
{
if (_instance == null) _instance = new StepRuleSetting();
_instance.Load();
return _instance;
}
}
public bool HasContainsKey(string stepType)
{
return DicRules.ContainsKey(stepType);
}
public List<string> GetStepRules(string stepType)
{
if (HasContainsKey(stepType))
{
Rules = DicRules[stepType];
if (Rules.Count <= 0)
{
MessageWindow.Show(MessageLevel.Warning, "No rules under this steptype: " + SettingsPath);
}
}
return Rules;
}
private void Load()
{
var xml = new XmlDocument();
try
{
xml.Load(Path.Combine(Environment.CurrentDirectory, "Settings", SettingsPath));
var ruleListRoot = xml.DocumentElement.SelectSingleNode("/RuleList");
if (ruleListRoot == null)
{
return;
}
var stepTypeLists = xml.DocumentElement.SelectNodes("/RuleList/StepType");
foreach (XmlNode node in stepTypeLists)
{
if (node.Attributes["name"] == null)
{
throw new ArgumentException("Required attribute 'name' is missing in configuration file " +
SettingsPath);
}
}
foreach (XmlNode node in stepTypeLists)
{
var stepRuleLists = node.ChildNodes;
List<string> rules = new List<string>();
foreach (XmlNode rule in stepRuleLists)
{
if (rule.Name == "Rule")
{
rules.Add(rule.InnerText);
}
}
if (!DicRules.ContainsKey(node.Attributes["name"].Value))
{
DicRules.Add(node.Attributes["name"].Value, rules);
}
}
}
catch (ArgumentException ex)
{
// Ignore the duplicate key.
MessageWindow.Show(MessageLevel.Warning, "There is configuration error in config file: " + SettingsPath);
}
catch (Exception ex)
{
var errMsg = string.Format("Errors occured while reading TreeNode setting from file [{0}].",
SettingsPath);
// Color list configuration failure does not affect functionality. Just show warnings.
// TODO: do localization for this message.
MessageWindow.Show(MessageLevel.Warning, "There is configuration error in config file: " + SettingsPath);
//Insert Exeption log
}
finally
{
xml = null;
}
}
}
}
加载XML文件到系统中的更多相关文章
- dom4j加载xml文件
## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...
- 通过JS加载XML文件,跨浏览器兼容
引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...
- 通过 XML HTTP 加载 XML 文件
新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="02-通 ...
- 解决dom4j加载xml文件性能慢的问题
在代码中使用: 1: DocumentHelper.parseText 2: SAXReader reader = new SAXReader(); Document extdocument = re ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- java中加载xml文件方法
this.getclass().getclassloader().getresourceasstream(String file); 可以加载文件,比如xml.
- tomcat与springmvc 结合 之---第19篇 springmvc 加载.xml文件的bean 过程
writedby 张艳涛,看springmvc 的源码太难了,怎么办, 这篇文章主要分析了看透springmvc的第9章结尾的 如何解析xml 命名空间标签 <?xml version=&quo ...
- 使用TreeView加载XML文件
PS: 由于小弟初学编程,本文只写实现方式,代码写的不是很好请见谅! 1.需要读取的xml文档内容 2. 最终实现效果 3 貌似看起实现起来很复杂 但是想想还是挺简单 思路: 读取XML文档 →获 ...
- 又一本springmvc学习指南 之---第22篇 springmvc 加载.xml文件的bean标签的过程
writedby 张艳涛,今天看spring mvc 学习指南的第2章,特意提下这个作者是how tomcat works 俩个作者之一, 喜欢上一本书的风格,使用案例来讲述原理, 在做第一个案例的时 ...
随机推荐
- visio连接线设置
以下画图是在新建空白模式的 默认的连线,如下图, 通过设置设计模式,可以把直角线编程直的线段, 设置如下图, 效果,如下图, 如果想在上面的图的线加箭头,可以一条一条加,但是,但是效率太低,我们可以通 ...
- Docker应用
1.tomcat容器创建 docker run -d --name Jdd_tomcat -p 8081:8080 tomcat [root@localhost etc]# docker run - ...
- MapReduce :基于 FileInputFormat 的 mapper 数量控制
本篇分两部分,第一部分分析使用 java 提交 mapreduce 任务时对 mapper 数量的控制,第二部分分析使用 streaming 形式提交 mapreduce 任务时对 mapper 数量 ...
- Windows 用bat脚本带配置启动redis,并用vb脚本使其在后台运行。
最近,在Windows上用开发PHP程序,需要用到Redis,每天要打开一个运行redis-server.exe的窗口这样比较烦,因为窗口就一直打开着,不能关闭. 所以就想着通过写脚本的方式,让他在后 ...
- JS模态框 简单案例
演示: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 特效 左右滑动轮播图jQuery思路
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- h5设计图尺寸
640 标准的话 设计图: 640*1136 body,html 背景图的话需要640*1008的 这样才能整屏刚刚好 750 标准的话 设计图: 750*1334 body,html背景图的话,75 ...
- Linux虚拟机的三种网络连接方式
Linux虚拟机的三种网络连接方式 虚拟机网络模式 无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host- ...
- day40-python多进程多线程-多线程实例和锁
多线程 线程是应用程序中工作的最小单元. 多线程是现实有两种方式:方法一:将要执行的方法作为参数传给Thread的构造方法(和多进程类似)t = threading.Thread(target=act ...
- Python中的lambda的简单介绍
在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.lambda是什么? 举个例子如下: func=lambda x:x+1 print(func(1)) p ...