简单的操作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
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
相关文章推荐
- c# winform app.config xml 配置文件 读写操作
- C#操作xml配置文件最简单方法
- struts2 简单注解配置代替xml配置文件
- 简单的XML操作:XML文件创建
- Android中使用XmlPullParser操作简单的xml文件
- Hibernate动态建表,通过hbm.xml配置文件创建数据表,进行数据库操作, 动态模型
- winForm *.Config配置文件的操作
- XML文件操作的简单类
- 一个操作配置文件(Reg,ini,XML)的类
- xml基本操作和保存配置文件应用实例
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- 编写XML作为配置文件的高级操作库
- C简单的配置文件操作
- winform中修改配置文件 同理可以是xml等其他
- Hibernate动态建表,通过hbm.xml配置文件创建数据表,进行数据库操作, 动态模型
- XML::Simple 简单的读写xml(尤其适合配置文件)
- HDFD 四个配置文件(core-site.xml hdfs-site.xml mapred-site.xml yarn-site.xml )的简单介绍
- 最简单的Spring配置文件模板——applicationContext.xml OR beans.xml
- 【MyBatis】一个简单的xml配置文件;
- 2. 数据库文件配置与简单操作 Model / M()