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. easyUI使用dailog实现弹出框带表单功能

    本文为博主原创,未经允许不得转载: 示例如下: 需要注意的地方在id为win的div中需要有:closed="true"这个属性,这个属性为控制dailogn对话框显示与隐藏的属性 ...

  2. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法

    mysql数据库连接出问题,提示超时,后来发现问题在于连接mysql数据库的jar包跟数据库版本不对应导致的,更换jar包一致就解决了.

  3. Thinkphph 使用RelationModel的三表关联查询机制

    有如下三个表 a表 b表 c表id bid other id cid other id other a表的bid关联b表的id,b表的cid关联c表的id 现在需要查询a表的时候顺带把b表和c表的相关 ...

  4. stylus笔记(三)

    1.@import 导入 Stylus支持字面@import CSS, 也支持其他Stylus样式的动态导入. 任何.css扩展的文件名将作为字面量.例如: @import "reset.c ...

  5. 在 .NET 项目中集成 SwaggerUI(2018.9.30)

    不多说,直接上教程! 1. 打开NuGet管理器搜索并安装 Swashbuckle和Swagger.Net两项 2. 修改生成设置 3. 修改SwaggerConfig文件 (1)去除注释 c.Inc ...

  6. AngularJS +Kendo UI Grid template

    var dataSource = new kendo.data.DataSource({ transport: { dataType: "json", read: inputUri ...

  7. 一天一点Zynq(1)xilinx-arm-linux交叉编译链 安装总结以及资源更新

    结束了对xilinx-arm-linux交叉编译链安装后,总结一下整个过程,方便后来的研究者们,少走点弯路. 关于xilinx-arm-linux交叉编译链的安装,网上一搜一大把,可是有的资料中的资源 ...

  8. vscode setting.jsonxx

    // Place your settings in this file to overwrite the default settings { "files.autoGuessEncodin ...

  9. Spring Boot + Spring Cloud 实现权限管理系统 (系统服务监控)

    系统服务监控 新建监控工程 新建Spring Boot项目,取名 kitty-monitor,结构如下. 添加项目依赖 添加 spring boot admin 的相关依赖. pom.xml < ...

  10. 面试题--Java

    &与&&区别? &与&&都是逻辑运算符,都是判断两边为真则为真,两边为假则为假,但是&&如果第一个条件不成立的话,后面的将不会再继续执行 ...