xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 基本信息配置 -->
<configurations>
<dataConfig>
<DataSource>dataSource</DataSource>
<Catalog>Northwind</Catalog>
<UserID>sa</UserID>
<Password>123</Password>
</dataConfig>
</configurations>
//读取xml
private void button1_Click(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader("e:/XMLFile1.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode dataSourc = doc.SelectSingleNode("/configurations/dataConfig/DataSource");
if (dataSourc != null)
{
textBox1.Text = dataSourc.InnerText;
}
XmlNode catalog = doc.SelectSingleNode("/configurations/dataConfig/Catalog");
if (catalog != null)
{
textBox2.Text = catalog.InnerText;
}
XmlNode userID = doc.SelectSingleNode("/configurations/dataConfig/UserID");
if (userID != null)
{
textBox3.Text = userID.InnerText;
}
XmlNode password = doc.SelectSingleNode("/configurations/dataConfig/Password");
if (password != null)
{
textBox4.Text = password.InnerText;
}
reader.Close();
}
//修改xml
private void button2_Click(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader("e:/XMLFile1.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode dataSourc = doc.SelectSingleNode("/configurations/dataConfig/DataSource");
if (dataSourc != null)
{
dataSourc.InnerText=textBox1.Text;
}
XmlNode catalog = doc.SelectSingleNode("/configurations/dataConfig/Catalog");
if (catalog != null)
{
catalog.InnerText = textBox2.Text;
}
XmlNode userID = doc.SelectSingleNode("/configurations/dataConfig/UserID");
if (userID != null)
{
userID.InnerText = textBox3.Text;
}
XmlNode password = doc.SelectSingleNode("/configurations/dataConfig/Password");
if (password != null)
{
password.InnerText = textBox4.Text;
}
reader.Close();
doc.Save("e:/XMLFile1.xml");
MessageBox.Show("Modify Succeed!", "information",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
转载地址:http://blog.sina.com.cn/s/blog_4c708e04010008kz.html