C#针对xml基本操作及保存配置文件应用实例
本文实例讲述了c#针对xml的基本操作及保存配置文件应用,分享给大家供大家参考。具体方法如下:
引言:这里首先介绍了xml的基本操作,后面写了一个经常用到的xml保存配置文件的实例。
xml常用方法:
定义xml文档:xmldocument xmldoc = new xmldocument();
初始化xml文档:xmldoc.load("d:\\book.xml");//找到xml文件
创建根元素:xmlelement xmlelement = xmldoc.createelement("", "employees", "");
创建节点:xmlelement xesub1 = xmldoc.createelement("title");
查找employees节点:xmlnode root = xmldoc.selectsinglenode("employees");
添加节点:xe1.appendchild(xesub1);
更改节点的属性:xe.setattribute("name", "李明明");
移除xe的id属性:xe.removeattribute("id");
删除节点title:xe.removechild(xe2);
1 创建xml文档
因为比较简单,直接写方法及结果。
{
xmldocument xmldoc = new xmldocument();
//加入xml的声明段落,<?xml version="1.0" encoding="gb2312"?>
xmldeclaration xmldeclar;
xmldeclar = xmldoc.createxmldeclaration("1.0", "gb2312", null);
xmldoc.appendchild(xmldeclar);
//加入employees根元素
xmlelement xmlelement = xmldoc.createelement("", "employees", "");
xmldoc.appendchild(xmlelement);
//添加节点
xmlnode root = xmldoc.selectsinglenode("employees");
xmlelement xe1 = xmldoc.createelement("node");
xe1.setattribute("name", "李明");
xe1.setattribute("isb", "2-3631-4");
//添加子节点
xmlelement xesub1 = xmldoc.createelement("title");
xesub1.innertext = "学习vs";
xe1.appendchild(xesub1);
xmlelement xesub2 = xmldoc.createelement("price");
xe1.appendchild(xesub2);
xmlelement xesub3 = xmldoc.createelement("weight");
xesub3.innertext = "20";
xesub2.appendchild(xesub3);
root.appendchild(xe1);
xmldoc.save("d:\\book.xml");//保存的路径
}
结果:
-<employees>-
<node isb="2-3631-4" name="李明">
<title>学习vs</title>-
<price>
<weight>20</weight>
</price>
</node>
</employees>
2 增加节点
xmldoc.load("d:\\book.xml");//找到xml文件
xmlnode root = xmldoc.selectsinglenode("employees");//查找employees节点
xmlelement xe1 = xmldoc.createelement("node2");//添加node2节点
xe1.setattribute("name", "张三");
xmlelement xesub1 = xmldoc.createelement("title");//定义子节点
xesub1.innertext = "心情好";
xe1.appendchild(xesub1);//添加节点到node2
root.appendchild(xe1);//添加节点到employees
xmldoc.save("d:\\book.xml");
结果:
-<employees>
-<node isb="2-3631-4" name="李明">
<title>学习vs</title>-
<price>
<weight>20</weight>
</price>
</node>-
<node2 name="张三">
<title>心情好</title>
</node2>-
<node2 name="张三">
<title>心情好</title>
</node2>
</employees>
3 修改节点:
{
xmldocument xmldocument = new xmldocument();
xmldocument.load("d:\\book.xml");
xmlnodelist nodelist = xmldocument.selectsinglenode("employees").childnodes;//获取employees节点的所有子节点
foreach (xmlnode xn in nodelist)//遍历
{
xmlelement xe = (xmlelement)xn;
if (xe.getattribute("name") == "李明")
{
xe.setattribute("name", "李明明");//更改节点的属性
xmlnodelist xnl = xe.childnodes;//获取xe的所有子节点
foreach (xmlnode xn1 in xnl)
{
xmlelement xe2 = (xmlelement)xn1;//将节点xn1的属性转换为xmlelement
if (xe2.name == "title")//找到节点名字为title的节点
{
xe2.innertext = "今天天气不好";
}
if (xe2.name == "price")
{
xmlnodelist xnl2 = xe2.childnodes;
foreach (xmlnode xn2 in xnl2)
{
if (xn2.name == "weight")
{
xn2.innertext = "88";
}
}
}
}
}
}
xmldocument.save("d:\\book2.xml");
}
运行结果:
-<employees>
-<node isb="2-3631-4" name="李明明">
<title>今天天气不好</title>-<price>
<weight>88</weight>
</price>
</node>
-<node2 name="张三">
<title>心情好</title>
</node2></employees>
4 删除节点:
{
xmldocument xmldocument = new xmldocument();
xmldocument.load("d:\\book1.xml");
xmlnodelist xnl = xmldocument.selectsinglenode("employees").childnodes;
foreach (xmlnode xn in xnl)
{
if (xn.name == "node")
{
xmlelement xe = (xmlelement)xn;//将xn的属性转换为xmlelement
xe.removeattribute("id");//移除xe的id属性
xmlnodelist xnl2 = xe.childnodes;
for (int i = 0; i < xnl2.count; i++)
{
xmlelement xe2 = (xmlelement)xnl2.item(i);
if (xe2.name == "title")
{
xe.removechild(xe2);//删除节点title
}
}
}
}
xmldocument.save("d:\\book3.xml");
}
结果:
-<employees>
-<node isb="2-3631-4" name="李明">-<price>
<weight>20</weight>
</price>
</node>-
<node2 name="张三">
<title>心情好</title>
</node2>-
<node2 name="张三">
<title>心情好</title>
</node2>
</employees>
前面介绍了xml的创建、节点的添加、节点的修改和删除,下面以写的一个保存项目配置文件的小例子。
举例说明:
首先在项目文件中创建一个xml文档:
<configurationn>
<serveraddress>1143</serveraddress>
<id>192.168</id>
</configurationn>
在保存配置文件时,最主要使用了两个方法:load和save。
load:初始化xml文档,以便项目文件获取具体的xml节点的值。
{
try
{
xmldocument xmldocument = new xmldocument();
xmldocument.load(path);
xmlnodelist xnl = xmldocument.selectsinglenode(managernode).childnodes;
foreach (xmlnode xn in xnl)
{
if (xn.name == configuration_serveraddress)
{
serveraddress = xn.innertext;
}
}
}
catch(exception ex)
{ }
}
save:在项目系统中进行修改配置文件值后,需要对xml进行重新保存
{
try
{
xmldocument xmldocument = new xmldocument();
xmldocument.load(path);
xmlnodelist xnl = xmldocument.selectsinglenode(managernode).childnodes;
foreach (xmlnode xn in xnl)
{
if (xn.name == configuration_serveraddress)
{
xn.innertext = serveraddress;
}
}
xmldocument.save(path);
}
catch (exception ex)
{ }
}
此处将所有代码都贴出来,方便下次实现。因为项目是wpf文件,而且都是简单控件,所以只贴出后台代码。
{
public const string managernode = "configurationn";//根节点
public const string configuration_serveraddress = "serveraddress";//子节点
private string _serveraddress;
public string serveraddress
{
get { return _serveraddress; }
set
{
_serveraddress = value;
notifypropertychanged("serveraddress");
}
}
public void load(string path)
{
try
{
xmldocument xmldocument = new xmldocument();
xmldocument.load(path);
xmlnodelist xnl = xmldocument.selectsinglenode(managernode).childnodes;
foreach (xmlnode xn in xnl)
{
if (xn.name == configuration_serveraddress)
{
serveraddress = xn.innertext;
}
}
}
catch(exception ex)
{ }
}
public void save(string path)
{
try
{
xmldocument xmldocument = new xmldocument();
xmldocument.load(path);
xmlnodelist xnl = xmldocument.selectsinglenode(managernode).childnodes;
foreach (xmlnode xn in xnl)
{
if (xn.name == configuration_serveraddress)
{
xn.innertext = serveraddress;
}
}
xmldocument.save(path);
}
catch (exception ex)
{ }
}
public event propertychangedeventhandler propertychanged;
private void notifypropertychanged(string propertyname)
{
if (propertychanged != null)
{
propertychanged(this, new propertychangedeventargs(propertyname));
}
}
public static configurationmanager instance = new configurationmanager();
}
public partial class mainwindow : window
{
public mainwindow()
{
initializecomponent();
start();
this.tb1.text = configurationmanager.instance.serveraddress.tostring();
}
private string path = "config\\system.xml";
private void button1_click(object sender, routedeventargs e)
{
configurationmanager.instance.serveraddress = this.tb1.text;
configurationmanager.instance.save(path);
}
private void button2_click(object sender, routedeventargs e)
{
application.current.shutdown();
}
private void start()
{
configurationmanager.instance.load(path);
}
}
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具:
希望本文所述对大家的c#程序设计有所帮助。