-
1.Xml文档如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<add key="IIDataBase" value="Server=192.168.10.12,2433;database=mang;user id=sa;password=2008sqlpwd;">
</add>
<add key="IIIDataBase" vaule="Server=192.168.10.22,6666;database=iii;user id=sa;password=nbPwd;">
</add>
<add key="RegisterDescription" value="用户根据系统提供的机器码经由算号器计算出注册号填入下框中进行产品注册。\r\n注:\r\n1.算号器由专人负责。\r\n2.如有其他问题联系管理员。">
</add>
<add key="RegisterCode" value="dsafds">
</add>
</configuration>2.定义一个类用来操作xml文档
public class Config
{
private XmlDocument xmlDoc; //引入命名空间:using System.Xml;
private string filePath = Application.StartupPath + @"\help\config.xml"; //引入命名空间:using System.Windows.Forms;public Config()
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
}
/// <summary>
/// 获取值
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
private string GetXmlValue(string strKey)
{
string strResult = "9999"; //找不到匹配的项;
for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
{
if (xmlDoc.DocumentElement.ChildNodes[i].Attributes[0].Value == strKey)
{
strResult = xmlDoc.DocumentElement.ChildNodes[i].Attributes[1].Value;
break;
}
}
return strResult;
}
/// <summary>
/// 修改值
/// </summary>
/// <param name="strKey"></param>
/// <param name="strValue"></param>
/// <returns></returns>
private string SetXmlValue(string strKey,string strValue)
{
string strResult = "9999"; //找不到匹配的项;
for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
{
if (xmlDoc.DocumentElement.ChildNodes[i].Attributes[0].Value == strKey)
{
xmlDoc.DocumentElement.ChildNodes[i].Attributes[1].Value = strValue;
strResult = "0"; //修改成功
xmlDoc.Save(filePath); //修改完后记得保存o(∩_∩)o
break;
}
}
return strResult;
}
}
-------------------------------------------------------------------------------------------------------------------------------
C#中如何操作XML,对XML进行增、删、改节点
增加节点
1.读取XML文件(加载XML文件)
XmlDocument doc = new XmlDocument();
doc.Load("student.xml");
2.查找根节点
XmlNode xn = doc.SelectSingleNode("student");
3.创建子节点student
XmlElement node = doc.CreateElement("student");
4.创建节点name、age、gender
XmlElement name = doc.CreateElement("name");
name.InnerText = "kunkun";
XmlElement age = doc.CreateElement("age");
age.InnerText = "20";
XmlElement gender = doc.CreateElement("gender");
gender.InnerText = "男";
5.将创建的节点,添加到二级节点student中
node.AppendChild(name);
node.AppendChild(age);
node.AppendChild(gender);
6.将二级节点添加到根节点中去
xn.AppendChild(node);
7.重新保存XML文件
doc.Save("student.xml");
修改节点
1.读取XML文件(加载XML文件)
XmlDocument doc = new XmlDocument();
doc.Load("student.xml");
2.查找根节点
XmlNode xn = doc.SelectSingleNode("student");
3.循环遍历,找到需要修改的节点,重新赋值
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.ChildNodes[0].InnerText == txtname.Text)
{
xn.ChildNodes[0].InnerText = txtname.Text;
xn.ChildNodes[1].InnerText = txtage.Text;
xn.ChildNodes[2].InnerText = ddrgender.Text;
}
}
4.重新保存XML文件
doc.Save("student.xml");
删除节点
1.读取XML文件(加载XML文件)
XmlDocument doc = new XmlDocument();
doc.Load("student.xml");
2.查找根节点
XmlNode xn = doc.SelectSingleNode("student");
3.循环遍历,找到需要修改的节点,然后删除
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.ChildNodes[0].InnerText == txtname.Text)
{
node.RemoveChild(xn);
}
}
4.重新保存XML文件
doc.Save("student.xml");
XML文件格式: <student> <student> <name>zhangsan</name> <age>18</age> <gender>男</gender> </student> <student> <name>lisi</name> <age>20</age> <gender>女</gender> </student> </student>
原文链接:/xml/299213.html