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 ...
随机推荐
- DAC calibrates 4- to 20-mA output current
Industrial controls make heavy use of 4- to 20-mA current loops to transmit process measurements bec ...
- Changing the Output Voltage of a Switching Regulator on the Fly
http://www.powerguru.org/changing-the-output-voltage-of-a-switching-regulator-on-the-fly/ There are ...
- MySQL审计功能
http://blog.itpub.net/29733787/viewspace-1604392/
- [js插件]分享一个文章内容信息提示插件Colortip
引用 项目中需要一个信息提示的功能,就上网找了一个插件,发现colortip实现比较简单,就定了这个插件. 实现过程 官网:http://tutorialzine.com/2010/07/colort ...
- wget 下载文件重进行命名
wget在下载的时候就重命名的: wget -c "www.baidu.com" -O baidu.index.html 保存输出日至,可以使用: wget -c "ww ...
- Git 报错:git - error: RPC failed; curl 18 transfer closed with outstanding read data remaining 解决方案
error: RPC failed; curl 18 transfer closed with outstanding read data remaining because have error w ...
- Sqlmap Tamper大全
sqlmap是一个自动化的SQL注入工具,其主要功能是扫描,发现并利用给定的URL的SQL注入漏洞,目前支持的数据库是MS-SQL,,MYSQL,ORACLE和POSTGRESQL.SQLMAP采用四 ...
- input 输入框默认获得焦点
JavaScript实现默认焦点: 如下写<body>标签: <body onload="window.formLogin.user.focus()"> & ...
- Ubuntu 16.04 LTS软件包管理基本操作
前文 Ubuntu 16.04 新特性中我们已经介绍过,随着 Ubuntu 16.04 LTS 的发布,Ubuntu 的软件包管理命令也发生了变化,新系统采用了 Debian 项目中所使用的 APT( ...
- 更改DNS轻松访问google.com,FaceBook,Youtube等
将默认的Dns更改为42.120.21.30即可打开 https://www.google.com/ https://www.facebook.com/ https://www.youtube.com ...