C#:XML操作(简单)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; using System.IO;
using System.Windows.Forms; namespace MyVertion
{
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 volatile 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()
{
return File.Exists(m_configPath);
} //保存、更改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;
}
else
{
xmlDoc.Load(m_configPath);
XmlNode root = xmlDoc.SelectSingleNode("Connection");
XmlNode xnd = root.SelectSingleNode(item);
return xnd.InnerText;
} } }
}
更多:https://i.cnblogs.com/EditPosts.aspx?postid=3673943
C#:XML操作(简单)的更多相关文章
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- XML系列之--对电文格式XML的简单操作(三)
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- T-Sql(五)xml操作
t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...
- 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】
一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...
- .net学习笔记---xml操作及读写
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- xml操作
一.LINQ to XML 编程基础 1.LINQ to XML类 System.Xml.Linq命名空间含有19个类,下表列出了它们的名称及其描述: 类 描述 XAttribute 表示一个 XML ...
- .Net XML操作 <第二篇>
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- 【转】python XML 操作总结(创建、保存和删除,支持utf-8和gb2312)
原文地址:http://hi.baidu.com/tbjmnvbagkfgike/item/6743ab10af43bb24f6625cc5 最近写程序需要用到xml操作,看了看python.org上 ...
- PHP XML操作的各种方法解析
PHP提供了一整套的读取 XML文件的方法,很容易的就可以编写基于 XML的脚本程序.本章将要介绍 PHP与 XML的操作方法,并对几个常用的 XML类库做一些简要介绍. XML是一种流行的半结构化文 ...
随机推荐
- Windows下配置Git服务器和客户端
http://www.cnblogs.com/lwme/archive/2012/12/25/configuring-git-server-and-client-on-windows.html] 选择 ...
- Boost Converter
Single Inductor Buck-Boost Converter in Tiny WCSP The TPS63036 is a non inverting buck-boost convert ...
- Springboot集成Jedis + Redisson(已自测)
原文:https://blog.csdn.net/c_zyer/article/details/79415728 本文主要跟大家分享在Springboot中集成Jedis和Redisson的方法.为什 ...
- 【实例图文详解】OAuth 2.0 for Web Server Applications
原文链接:http://blog.csdn.net/hjun01/article/details/42032841 OAuth 2.0 for Web Server Applicatio ...
- MySql_SQLyog+SQL Assistant实现智能提示
相信用过sqlserver+SQL Assistant的同学都知道其智能提示多么方便,但是转到mysql后,无论是使用navicat还是webbench都无法实现较好的智能提示效果, 最终在网上找到使 ...
- 深入解析OkHttp3
OkHttp是一个精巧的网络请求库,有如下特性: 1)支持http2,对一台机器的所有请求共享同一个socket 2)内置连接池,支持连接复用,减少延迟 3)支持透明的gzip压缩响应体 4) ...
- Android ListView工作原理完全解析,带你从源码的角度彻底理解
版权声明:本文出自郭霖的博客,转载必须注明出处. 目录(?)[+] Adapter的作用 RecycleBin机制 第一次Layout 第二次Layout 滑动加载更多数据 转载请注明出处:h ...
- JQuery与JavaScript onload的区别
1.window.onload 不能有多个,后面的功能会覆盖前面.而jQuery(document).ready()可以存在多个. 2.window.onload 在页面所有元素(包括图片,引用文件) ...
- easyui radio 类型的取值和赋值方法
1.HTML 文件 <tr id="client_check1"> <th>委托人证件类型:</th> <td><input ...
- docker学习总结--安装、卸载
参考:http://blog.csdn.net/u012562943/article/details/50463400 https://docs.docker.com/engine/getstarte ...