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操作的更多相关文章

  1. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  2. MySQL数据库(2)- 库的操作、表的操作、数据的操作、存储引擎的介绍

    一.库的操作 1.系统数据库 执行如下命令,查看系统数据库: mysql> show databases; 参数解释: information_schema: 虚拟库,不占用磁盘空间,存储的是数 ...

  3. 传智播客JavaWeb day09-mysql入门、数据库操作、数据库表操作、数据行操作

    不知不觉已到了第九天了,今天主要讲了关系数据库的基本概述.安装.数据库.表和数据行的操作 1. 基本概述 1.1 数据库就是用来存储数据的.早期是存在文件里面的操作起来效率低而且不是很安全. 1.2 ...

  4. 使用dom4j处理xml操作xml数据

    使用dom4j处理xml操作xml数据 示例代码: public class TestDom4j { public static void main(String[] args) { String x ...

  5. Open XML操作Excel导入数据

    项目中发现使用OleDb(using System.Data.OleDb)相关对象处理Excel导入功能,不是很稳定经常出问题,需要把这个问题解决掉.项目组提出使用OpenXML来处理Excel的导入 ...

  6. PHP XML操作的各种方法解析

    PHP提供了一整套的读取 XML文件的方法,很容易的就可以编写基于 XML的脚本程序.本章将要介绍 PHP与 XML的操作方法,并对几个常用的 XML类库做一些简要介绍. XML是一种流行的半结构化文 ...

  7. PowerShell 数组以及XML操作

    PowerShell基础 PowerShell数组操作 将字符串拆分成数据的操作 cls #原始字符串 $str = "abc,def,ghi,mon" #数据定义 #$StrAr ...

  8. T-Sql(五)xml操作

    t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...

  9. XML格式示例 与 XML操作(读取)类封装

    header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...

随机推荐

  1. centos 7 查看所有登录用户的操作历史

    2019-01-07 转自  https://www.cnblogs.com/kevingrace/p/7373146.html centos 7 查看所有登录用户的操作历史 在Linux系统的环境下 ...

  2. winform FormBordStyle=none 及 wpf FormBordStyle=none 的鼠标点击移动问题

    winform: //bool formMove = false;//窗体是否移动 //Point formPoint;//记录窗体的位置 private void Login_MouseDown(o ...

  3. linux mint 19安装最新社区版docker

    sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...

  4. Struts1原理解析

    1.浏览器发送http请求->web服务器. 2.web服务器将 请求进行解析. 3.web服务器解析后将请求转发给ActionServelet(总队长). 3.查询struts-config. ...

  5. (转)在 CentOS7 上安装 MongoDB

    在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录: cd /usr/local 3 在当前目录下创建 ...

  6. 饶军:Apache Kafka的过去,现在,和未来

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 大家好,我大概简单的介绍一下,我叫饶军,我是硅谷的初创公司Confluent的联合创始人之一,我们公 ...

  7. Hibernate 4.3 SessionFactory

    Configuration configuration = new Configuration().configure(); //以下这两句就是4.3的新用法 StandardServiceRegis ...

  8. URL重写html后Html文件打不开解决办法

    1.首先照旧在网站配置的应用程序扩展名映射中添加扩展名.html映射到aspnet_isapi.dll,是否存在不选: 2.在web.config文件中<compilation>节点下添加 ...

  9. JAVA标签

    java没有GOTO,可以通过标签实现跳转. 在 Java 里唯一需要用到标签的地方就是拥有嵌套循环,而且想中断或继续多个嵌套级别的时候. 使用位置:用在循环语句之前.----它实际需要紧靠在循环语句 ...

  10. 超赞的 Go 语言 INI 文件操作

    灵活的数据源 不光光可以从文件读取配置,还支持 []byte 类型的纯数据读取和基于 io.ReadCloser 的流式读取. 多种格式兼容 各种文件种类的广泛支持,包括但不限于 my.cnf..gi ...