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

  1. 【.NET】XML文件的创建,修改,删除

    类名:XML /// 1.创建XML文档 /// 2.在根节点下增加子元素 /// 3.在元素下增加子元素 /// 4.获取类型为制定值的一组节点 /// 5.抓取网页上的xml文档赋值给XmlDoc ...

  2. Qt之QDomDocument操作xml文件-模拟ini文件存储

    一.背景 不得不说Qt是一个很强大的类库,不管是做项目还是做产品,Qt自身封装的东西就已经非常全面了,我们今天的这篇文章就是模拟了Qt读写ini文件的一个操作,当然是由于一些外力原因,我们决定自己来完 ...

  3. C#对XML操作类

    C#对XML操作类 该类包含了对XML文件的创建,添加,读取,删除,修改等操作 //#define isUnity #if isUnity using UnityEngine; #endif usin ...

  4. 像使用数据库一样使用xml

    目录 xml简介 语言结构 tinyxml 下载tinyxml 应用实例 tinyxml的封装 封装源码 实际应用 xml简介 XML 没什么特别的.它仅仅是纯文本而已.有能力处理纯文本的软件都可以处 ...

  5. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  6. C#:XML操作(简单)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  7. xml去除指定节点,保留子节点操作

    现有xml文件: <?xml version="1.0" encoding="utf-8" ?> <ShiftConfig xmlns:xsi ...

  8. C# XML 操作

    1 xml文件格式 <?xml version="1.0" encoding="utf-8"?> <userInfo> <user ...

  9. java中properties的使用实例

    package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...

随机推荐

  1. Level-shifting nixes need for dual power supply

    The AD736 true-rms-to-dcconverter is useful for many applications that require precise calculation o ...

  2. FreeRTOS API

    Task Creation xTaskCreate vTaskDelete Task Control vTaskDelay vTaskDelayUntil uxTaskPriorityGet vTas ...

  3. STM32F10X FLASH and SRAM size

    STM32F10X FLASH Size SRAM Size Device Type --------------------------------------------------------- ...

  4. DotNet.Utilities工具类

    来源:http://git.oschina.net/kuiyu/dotnetcodes/tree/master/DotNet.Utilities

  5. springBoot 2.X-自定义拦截器

    package com.cx.springboot.myInter; import javax.servlet.http.HttpServletRequest; import javax.servle ...

  6. Enterprise Library:Unity的几个注意事项

    背景 在.Net平台中,几乎所有的Ioc容器在注册方面都不一致,使用Unity需要注意几个事项,咱们通过实验进行验证一下. 验证的内容: 集合的获取. 生命周期管理. 实验 代码 1 using Sy ...

  7. 实现SQL Server中的切割字符串SplitString函数

    有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.没什么好说的,需要的朋友直接拿去用吧 SET ANSI_NULLS ON GO S ...

  8. 跨平台app开发(引擎)工具的选择

    1.html5执行速度慢,用户体验不好 2.原生应用开发,即ios和安卓分别开发,需要两种技术人员,后期代码维护困难,很难达到统一. 3.xamarin是一款c#的解决方案,收费.xamarin.fo ...

  9. Packagist / Composer 中国全量镜像

    用法: 有两种方式启用本镜像服务: 将配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1 (推荐方式)” 将配置信息添加到单个项目的 composer. ...

  10. IIs 中运行asp程序出现“An error occurred on the server when processing the URL. Please contact the system administrator.”错误

    在个人的win08系统上使用IIs运行 asp程序结果出现了以下错误 An error occurred on the server when processing the URL. Please c ...