C# XMLOperate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; using System.IO;
using System.Windows.Forms; namespace ResultsManage
{
class XMLOperate
{
private string m_configPath = Application.StartupPath + @"\DatabaseConfig.xml";
private string vertion = "1.0";
private string encoding = "UTF-8";
private string standalone = "no";
private string comment = "database config"; private XmlDocument xmlDoc = null; private static XMLOperate XmlOp = null;
public static XMLOperate GetInstance()
{
if (null == XmlOp)
{
XmlOp = new XMLOperate(null);
}
return XmlOp;
} public string _Vertion
{
set
{
vertion = value;
} get
{
return vertion;
}
} public string _Encoding
{
set
{
encoding = value;
} get
{
return encoding;
}
} public string _Standalone
{
set
{
standalone = value;
} get
{
return standalone;
}
} public string _Comment
{
set
{
comment = value;
} get
{
return comment;
}
} public string _ConfigPath
{
get
{
return m_configPath;
}
} #region 构造函数
public XMLOperate(string xmlPath)
{
if (!string.IsNullOrEmpty(xmlPath))
{
m_configPath = xmlPath;
}
xmlDoc = new XmlDocument();
}
#endregion //创建configxml文件
public void CreateConfigXml()
{
xmlDoc.CreateXmlDeclaration(vertion, encoding, standalone);
xmlDoc.CreateComment(comment); XmlElement rootEle = xmlDoc.CreateElement("Connection");
xmlDoc.AppendChild(rootEle); XmlElement ele = xmlDoc.CreateElement("Server");
rootEle.AppendChild(ele);
ele = xmlDoc.CreateElement("Instance");
rootEle.AppendChild(ele);
ele = xmlDoc.CreateElement("Database");
rootEle.AppendChild(ele);
ele = xmlDoc.CreateElement("UserName");
rootEle.AppendChild(ele);
//ele = xmlDoc.CreateElement("Password");
//rootEle.AppendChild(ele);
ele = xmlDoc.CreateElement("Vertion");
rootEle.AppendChild(ele); xmlDoc.Save(m_configPath); //加入XML的声明段落,<?xml version="1.0" encoding="utf-8"?>
// xmlDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
// <Connection>
// </Connection>");
// XmlNode root = xmlDoc.SelectSingleNode("Connection");
} //检查配置文件是否存在
public bool IsExist()
{
if(!File.Exists(m_configPath))
{
return false;
}
return true;
} //保存、更改xml文件
public bool UpdateConfigInfo(string server, string instance, string database, string username, string password, string version)
{
if (!IsExist())
{
return false;
}
XmlNode root = xmlDoc.SelectSingleNode("Connection");
XmlNode xnd = root.SelectSingleNode("Server");
xnd.InnerText = server;
xnd = root.SelectSingleNode("Instance");
xnd.InnerText = instance;
xnd = root.SelectSingleNode("Database");
xnd.InnerText = database;
xnd = root.SelectSingleNode("UserName");
xnd.InnerText = username;
//xnd = root.SelectSingleNode("Password");
//xnd.InnerText = password;
xnd = root.SelectSingleNode("Vertion");
if (!string.IsNullOrEmpty(version))
{
xnd.InnerText = version;
} xmlDoc.Save(m_configPath);
return true;
} /// <summary>
/// 读配置文件
/// </summary>
/// <returns></returns>
public string ReadConfigInfo(string item)
{
if (!IsExist())
{
MessageBox.Show("配置文件不存在!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
} xmlDoc.Load(m_configPath);
XmlNode root = xmlDoc.SelectSingleNode("Connection");
XmlNode xnd = root.SelectSingleNode(item);
return xnd.InnerText;
} }
}
C# XMLOperate的更多相关文章
- 【.NET】XML文件的创建,修改,删除
类名:XML /// 1.创建XML文档 /// 2.在根节点下增加子元素 /// 3.在元素下增加子元素 /// 4.获取类型为制定值的一组节点 /// 5.抓取网页上的xml文档赋值给XmlDoc ...
- Qt之QDomDocument操作xml文件-模拟ini文件存储
一.背景 不得不说Qt是一个很强大的类库,不管是做项目还是做产品,Qt自身封装的东西就已经非常全面了,我们今天的这篇文章就是模拟了Qt读写ini文件的一个操作,当然是由于一些外力原因,我们决定自己来完 ...
- C#对XML操作类
C#对XML操作类 该类包含了对XML文件的创建,添加,读取,删除,修改等操作 //#define isUnity #if isUnity using UnityEngine; #endif usin ...
- 像使用数据库一样使用xml
目录 xml简介 语言结构 tinyxml 下载tinyxml 应用实例 tinyxml的封装 封装源码 实际应用 xml简介 XML 没什么特别的.它仅仅是纯文本而已.有能力处理纯文本的软件都可以处 ...
- [No0000DE]C# XmlHelper XML类型操作 类封装
using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...
- C#:XML操作(简单)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- xml去除指定节点,保留子节点操作
现有xml文件: <?xml version="1.0" encoding="utf-8" ?> <ShiftConfig xmlns:xsi ...
- C# XML 操作
1 xml文件格式 <?xml version="1.0" encoding="utf-8"?> <userInfo> <user ...
- java中properties的使用实例
package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...
随机推荐
- 用C++/CLI搭建C++和C#之间的桥梁(一)—— 简介
C#和C++是非常相似的两种语言,然而我们却常常将其用于两种不同的地方,C#得益于其简洁的语法和丰富的类库,常用来构建业务系统.C++则具有底层API的访问能力和拔尖的执行效率,往往用于访问底层模块和 ...
- Spring MVC @ModelAttribute 详解
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...
- Source Insight常用快捷键及注释快捷键设置
转:http://blog.csdn.net/tlaff/article/details/6536610 在使用SI过程中,我根据自己的使用习惯修改了它的默认快捷键,并且在配置文件中添加了一些人性化功 ...
- jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)
http://cuisuqiang.iteye.com/blog/1480525 模糊查询like要这样写 注意Object参数和like语法 public static void main( ...
- 使用 pm2-web 监控 pm2 服务运行状态
pm2-web 是一款 pm2 服务状态监控程序,基于 web . 安装 $ npm install -g pm2-web 运行(默认是在8080端口) $ pm2-web 配置 pm2-web 将会 ...
- HTML+CSS网站开发兵书
<HTML+CSS网站开发兵书> 基本信息 作者: 高洪涛 丛书名: 程序员藏经阁 出版社:电子工业出版社 ISBN:9787121212369 上架时间:2013-8-26 出版日期:2 ...
- C++中 使用数组作为map容器VAlue值的解决方法
1)是用Vector容器代替数组 2)使用数组指针(需要注意局部变量的问题,指针是否需要用new创建) int red [ 3 ] = { 1 , 0 , 0 }; int green [ 3 ] ...
- Visual Studio 2017各版本离线安装包获取以及安装教程
系统: windows 7旗舰版 前言: Visual Studio 2017版本与以往的2015.2013.2012版本不同,采用了新的模块化安装方法.微软官方也并未提供ISO镜像,作者根据官方提 ...
- python2 python3编码问题记录
最近在服务器上跑脚本,linux自带的是python 2.x,中文显示经常有问题,通过下面两篇终于弄懂了. https://www.cnblogs.com/575dsj/p/7112767.html ...
- vb.net中将DataGridView与数据源绑定
在< .net中将DataGridView内的数据导出为Excel表格>中说了如何导出数据到Excel,今天这篇文章将讲述如何绑定数据源,在控件中显示我们需要的信息. 在敲机房收费系统的时 ...