xml文件如下:
利用C#读写代码如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace WindowsApplication1
{
class XmlOperator
{
private string xmlPath;
private XmlDocument myDc=new XmlDocument();
///
/// 装载配置文件
///
/// 配置文件的路径
public XmlOperator(string Path)
{
xmlPath = Path;
try
{
myDc.Load(xmlPath);
}
catch
{
MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
///
/// 将配置文件中的值读入
///
public void GetXmlReader()
{
PublicData.DBPath = myDc.SelectSingleNode("system").SelectSingleNode("DBPath").InnerText;
PublicData.BackPicturePath = myDc.SelectSingleNode("system").SelectSingleNode("BackPicturePath").InnerText;
PublicData.MIDPath = myDc.SelectSingleNode("system").SelectSingleNode("MIDPath").InnerText;
PublicData.DBName = myDc.SelectSingleNode("system").SelectSingleNode("DBName").InnerText;
PublicData.DBPassword = myDc.SelectSingleNode("system").SelectSingleNode("DBPassword").InnerText;
}
///
/// 将自己的设置写入配置文件
///
///
public void SetXmlFile()
{
myDc.SelectSingleNode("system").SelectSingleNode("DBPath").InnerText = PublicData.DBPath;
myDc.SelectSingleNode("system").SelectSingleNode("BackPicturePath").InnerText = PublicData.BackPicturePath;
myDc.SelectSingleNode("system").SelectSingleNode("MIDPath").InnerText = PublicData.MIDPath;
myDc.SelectSingleNode("system").SelectSingleNode("DBName").InnerText = PublicData.DBName;
myDc.SelectSingleNode("system").SelectSingleNode("DBPassword").InnerText = PublicData.DBPassword;
myDc.Save(xmlPath);
}
}
}