本文实例讲述了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");//保存的路径
}</div>
结果:
-<Employees>-
<Node ISB="2-3631-4" Name="李明">
<title>学习VS</title>-
<price>
<weight>20</weight>
</price>
</Node>
</Employees></div>
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");</div>
结果:
-<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></div>
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");
}</div>
运行结果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明明">
<title>今天天气不好</title>-<price>
<weight>88</weight>
</price>
</Node>
-<Node2 Name="张三">
<title>心情好</title>
</Node2></Employees></div>
4 删除节点: