您的位置:首页 > 其它

简单的操作xml ,用于在winform中配置文件.

2010-02-02 08:18 363 查看
写内容到配置文件中.
try{
ArrayList str = new ArrayList();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("config.xml"); //config.xml是一个配置文件
XmlNode node = xmlDoc.DocumentElement;
XmlNode appNote = node.SelectSingleNode("appSettings");
XmlNodeList appNoteList = appNote.ChildNodes;
str.Add(txtSqlServer.Text); //获取界面上的配置信息
str.Add(txtUser.Text);
str.Add(txtPassword.Text);
str.Add(txtDb.Text);
int i=0;
foreach(XmlNode tmpNote in appNoteList)
{
tmpNote.Attributes["value"].Value = str[i].ToString(); //传到xml中.
i++;
}
xmlDoc.Save("config.xml");
this.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
//读内容.
try{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("config.xml");
XmlNode node = xmlDoc.DocumentElement;
XmlNode appNote = node.SelectSingleNode("appSettings");
XmlNodeList appNoteList = appNote.ChildNodes;
MessageBox.Show(appNote.ChildNodes[2].Attributes["value"].Value);
foreach(XmlNode tmpNote in appNoteList)
{
MessageBox.Show(tmpNote.Attributes["value"].Value);
}
xmlDoc.Save("config.xml");
this.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
config.xml中的内容.
<?xml version="1.0"?>
<config>
<appSettings>
<add key="SQLServerIPorName" value="192.168.0.99" />
<add key="SQLServerUser" value="sa" />
<add key="SQLServerPwd" value="" />
<add key="SQLServerDB" value="PowerCase" />
</appSettings>
</config>
我用的平台是SharpDevelop1.1的 XML文件是我添加的一个空的文件. 注意路径 config.xml 在debug 下面.本文出自 “知识就是财富” 博客,请务必保留此出处http://lovefly.blog.51cto.com/914912/273231
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: