c# 操作临时数据---XML操作
class Config
{
static string path;
/// <summary>
/// 配置文件的路径
/// </summary>
public static string Path
{
get { return path; }
set { path = value; }
}
private XmlDocument xml;
private static Config instance; /// <summary>
/// 单件实例
/// </summary>
public static Config Instance
{
get
{
if (instance == null)
instance = new Config();
return instance;
}
} private Config()
{
items = new Hashtable();
xml = new XmlDocument();
if (!System.IO.File.Exists(path))
{
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
xml.AppendChild(xml.CreateElement("Config"));
try
{
string dir = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(dir))
System.IO.Directory.CreateDirectory(dir);
}
catch { }
}
else
{
try
{
xml.Load(path);
}
catch (XmlException)
{
//如果 Xml 读取失败, 则重新创建
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
xml.AppendChild(xml.CreateElement("Config"));
}
}
} /// <summary>
/// 储存
/// </summary>
public void Save()
{
foreach (object key in items.Keys)
{
XmlNode node = xml.DocumentElement.SelectSingleNode("Item[@Name='" + key.ToString() + "']");
if (node == null)
{
node = xml.CreateElement("Item");
XmlAttribute att = xml.CreateAttribute("Name");
att.Value = key.ToString();
node.Attributes.Append(att);
xml.DocumentElement.AppendChild(node);
}
node.InnerText = items[key].ToString();
} xml.Save(path);
items.Clear();
} /// <summary>
/// 读取和存储
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this[string key]
{
get
{
if (items.Contains(key))
{
return items[key].ToString();
}
else
{
XmlNode node = xml.DocumentElement.SelectSingleNode("Item[@Name='" + key + "']");
if (node == null)
return null;
return node.InnerText;
}
}
set
{
items[key] = value;
}
} /// <summary>
/// 储存值的链表
/// </summary>
private Hashtable items;
}
读取xml内容:
Config.Path = Path.Combine(Environment.CurrentDirectory, "config.xml");
runTime = Convert.ToDateTime(Config.Instance["RunTime"]); //获取每日运行时间
todayDone = Config.Instance["TodayDone"]; //获取今日是否完成的标识
写入:
#region 修改配置文件
public static bool ChangeConfig(string AppKey, string AppValue)
{
Config.Instance[AppKey] = AppValue;
Config.Instance.Save();
return true;
}
#endregion
ChangeConfig("TodayDone", "");
c# 操作临时数据---XML操作的更多相关文章
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- MySQL数据库(2)- 库的操作、表的操作、数据的操作、存储引擎的介绍
一.库的操作 1.系统数据库 执行如下命令,查看系统数据库: mysql> show databases; 参数解释: information_schema: 虚拟库,不占用磁盘空间,存储的是数 ...
- 传智播客JavaWeb day09-mysql入门、数据库操作、数据库表操作、数据行操作
不知不觉已到了第九天了,今天主要讲了关系数据库的基本概述.安装.数据库.表和数据行的操作 1. 基本概述 1.1 数据库就是用来存储数据的.早期是存在文件里面的操作起来效率低而且不是很安全. 1.2 ...
- 使用dom4j处理xml操作xml数据
使用dom4j处理xml操作xml数据 示例代码: public class TestDom4j { public static void main(String[] args) { String x ...
- Open XML操作Excel导入数据
项目中发现使用OleDb(using System.Data.OleDb)相关对象处理Excel导入功能,不是很稳定经常出问题,需要把这个问题解决掉.项目组提出使用OpenXML来处理Excel的导入 ...
- PHP XML操作的各种方法解析
PHP提供了一整套的读取 XML文件的方法,很容易的就可以编写基于 XML的脚本程序.本章将要介绍 PHP与 XML的操作方法,并对几个常用的 XML类库做一些简要介绍. XML是一种流行的半结构化文 ...
- PowerShell 数组以及XML操作
PowerShell基础 PowerShell数组操作 将字符串拆分成数据的操作 cls #原始字符串 $str = "abc,def,ghi,mon" #数据定义 #$StrAr ...
- T-Sql(五)xml操作
t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...
- XML格式示例 与 XML操作(读取)类封装
header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...
随机推荐
- c#正则表达式--环视
网上有一篇文章写的很好.请参见 正则表达式之环视 .现将自己的理解记录下来. 环视(lookaround)可以理解为限定条件.即字符串中,某个字符左边或右边能出现什么,不能出现什么.只是一个 ...
- Openerp 添加修改报表
Report Designer 模块在生成新报表的时候是有BUG的不建议直接使用,不过我们也可以通过该插件再写简单的代码来实现新添加报表,插件安装成功后我们可以按照下列方法来添加报表 OpenERP ...
- 关闭mac自带apache的启动。
关闭mac自带apache的启动. sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 如果哪天 ...
- VS2015 MSVCRTD.lib(_chandler4gs_.obj) : error LNK2019: unresolved external symbol __except_handler4_common referenced in function __except_handler4
今天在VS2015中用编译好的QT5静态库打包软件,配置好QT的静态环境后, 发现报MSVCRTD.lib(_chandler4gs_.obj) : error LNK2019: unresolved ...
- GitHub与Git指令入门
GitHub入门 创建账户 之前做代码版本控制都是在bitbucket上面的私有仓库.现在开源社区挺火的,想在github找些开源的工程学习一下,于是加入了github,这里记录一下入门的经历. 首先 ...
- Hive的Shell里hive> 执行操作时,出现FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask错误的解决办法(图文详解)
不多说,直接上干货! 这个问题,得非 你的hive和hbase是不是同样都是CDH版本,还是一个是apache版本,一个是CDH版本. 问题详情 [kfk@bigdata-pro01 apache-h ...
- javascript中的function 函数名(){} 和 函数名:function(){}有什么不同
function functionName(){};这是定义一个函数 functionName:function(){};是设置一个对象的方法. 下面举一个例子: <html> <h ...
- HaspMap的新奇用法
HashMap<String, String> map = new HashMap<String,String>(){ private static final long se ...
- spring之IOC模拟实现
使用Spring框架已经有很长时间了,一直没有仔细的想过框架的设计思想是什么样的,底层到底是怎么实现的,这几天调试程序看了些源码,写下来做个记录.由于Spring框架博大精深,个人理解的难免有不正确的 ...
- Java将Excel的列数以字母表示的字符串转换成数字表示
我们知道,在 Excel 中,行数用数字表示,而列数是用字母表示的(如下图所示),有时候需要把它转换成数字来使用,或者把数字转换成字母.(例如使用POI操作Excel) 下面是转换代码,用来进行字母和 ...