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

  1. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  2. XML系列之--对电文格式XML的简单操作(三)

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  3. 简单的XML操作类

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

  4. T-Sql(五)xml操作

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

  5. 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】

    一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...

  6. .net学习笔记---xml操作及读写

    一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...

  7. xml操作

    一.LINQ to XML 编程基础 1.LINQ to XML类 System.Xml.Linq命名空间含有19个类,下表列出了它们的名称及其描述: 类 描述 XAttribute 表示一个 XML ...

  8. .Net XML操作 <第二篇>

    一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...

  9. 【转】python XML 操作总结(创建、保存和删除,支持utf-8和gb2312)

    原文地址:http://hi.baidu.com/tbjmnvbagkfgike/item/6743ab10af43bb24f6625cc5 最近写程序需要用到xml操作,看了看python.org上 ...

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

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

随机推荐

  1. Python模块之: configobj(转)

    原来也有写过一篇文章Python模块之: ConfigParser 用来解析INI文件,但是在使用过程中存在一些问题.比如:1,不能区分大小写.2,重新写入的ini文件不能保留原有INI文件的注释.3 ...

  2. Spring集合 (List,Set,Map,Properties) 实例

    下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...

  3. wpf 分别用 xaml 和后台代码实现 色彩渐变

    xaml 方法: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.micros ...

  4. .NET 4.5 is an in-place replacement for .NET 4.0

    With the betas for .NET 4.5 and Visual Studio 11 and Windows 8 shipping many people will be installi ...

  5. Visual SVN 企业版代码管理平台的建设

    通常需要完整的SVN的代码管理平台系统的搭建,需要安装三个文件,Visual SVN server  , TortoiseSVN, Visual SVN. Visual SVN server  企业版 ...

  6. 微信公众平台——获取access_token、expires_in

    微信公众平台——获取access_token.expires_in 在微信公众平台接口开发中,Access Token占据着重要地位,它相当于进入各种接口的邀请,拿到这个钥匙才拥有调用其他各种特殊接口 ...

  7. js:对象的创建(为prototype做铺垫)

    /**  *在js中并不存在类,所以能够直接通过Object来创建对象,可是使用这样的方式创建有一  *弊端:因为没有类的约束,无法实现对象的反复利用,而且没有一种规范约定,在操作时easy带来问题. ...

  8. java jdk查看源代码

    事实上假设你安装了JDK的话,你就已经拥有了java api的源代码. 安装JDK文件夹下的src.zip文件就是java api的源代码. 比方:C:\Program Files\Java\jdk1 ...

  9. QT 5.3 VS2010 中文

    #include <QtWidgets/QApplication> #include <QTextCodec>#include <QLabel>#pragma ex ...

  10. opencv CxImage 互转 (Mat)

    //to Mat CxImage img; img.Load("C:\\f.jpg"); uint8_t* buf=NULL; int32_t len=0; bool rs =im ...