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文件到系统中的更多相关文章

  1. dom4j加载xml文件

    ## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...

  2. 通过JS加载XML文件,跨浏览器兼容

    引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...

  3. 通过 XML HTTP 加载 XML 文件

    新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="02-通 ...

  4. 解决dom4j加载xml文件性能慢的问题

    在代码中使用: 1: DocumentHelper.parseText 2: SAXReader reader = new SAXReader(); Document extdocument = re ...

  5. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  6. java中加载xml文件方法

    this.getclass().getclassloader().getresourceasstream(String file); 可以加载文件,比如xml.

  7. tomcat与springmvc 结合 之---第19篇 springmvc 加载.xml文件的bean 过程

    writedby 张艳涛,看springmvc 的源码太难了,怎么办, 这篇文章主要分析了看透springmvc的第9章结尾的 如何解析xml 命名空间标签 <?xml version=&quo ...

  8. 使用TreeView加载XML文件

    PS: 由于小弟初学编程,本文只写实现方式,代码写的不是很好请见谅! 1.需要读取的xml文档内容 2. 最终实现效果 3  貌似看起实现起来很复杂 但是想想还是挺简单 思路:  读取XML文档 →获 ...

  9. 又一本springmvc学习指南 之---第22篇 springmvc 加载.xml文件的bean标签的过程

    writedby 张艳涛,今天看spring mvc 学习指南的第2章,特意提下这个作者是how tomcat works 俩个作者之一, 喜欢上一本书的风格,使用案例来讲述原理, 在做第一个案例的时 ...

随机推荐

  1. HTML基础【1】:认识 HTML

    什么是HTML ​ HTML其实是 HyperText Markup Language 的缩写,超文本标记语言 HTML的作用 1.首先利用记事本保存了一个标题和两段描述, 然后修改纯文本文件的扩展名 ...

  2. 使用vue.js + laravel开发单页面应用

    最近学了vuejs和laravel,然后顺便就撸了简单的demo,这里将会根据这个demo介绍下如何使用vuejs+laravel开发一个简单的单页面应用,demo的github地址是https:// ...

  3. Linux下更新Git

     查看git版本,卸载旧版本(如果没有安装git请直接到下一步) git --version yum remove git  安装依赖软件 yum install curl-devel expat-d ...

  4. 『Python CoolBook』C扩展库_其六_线程

    GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...

  5. selenium选择器_css属性选择器

    搜索 <button class="btn-search tb-bg" type="submit" data-spm-click="gostr= ...

  6. django项目的部署

    django项目部署到云服务器: 0.通过xshell连接远程主机服务器ip 1.使用xftp将项目发送到服务器端(也可以使用git) a.路径推荐为/var/project/(项目名) 2.给服务器 ...

  7. OpenResty安装使用教程(CentOS 6)

    一.安装OpenResty Linux官方建议直接通过官方提供的预编译包安装:http://openresty.org/cn/linux-packages.html # 确保yum周边工具已经安装 y ...

  8. unity中将多个图片进行椭圆排序

    //保存需要排序的精灵容器 public GameObject[] Sprites; public Transform centrePoint;//椭圆的中心点 ;//每个方块间的角度偏移 //保存位 ...

  9. 为什么企业需要IT资产管理

    为什么企业需要IT资产管理 为什么企业需要IT资产管理?其实这个问题,如同企业为什么要管理一样重要. 企业需要管理.因为有了管理,人.物可尽其用,在不同的位置.角色上发挥作用,然后按照一定的运维规则, ...

  10. jq demo 点击评分组件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...