浅用xml
来源:互联网 发布:知进退明得失是谁说的 编辑:程序博客网 时间:2024/05/21 08:27
用一个xml文件来存放一些你要修改的数据。比如你要修改时间Time。ShowDialog()的时候,将修改的值通过xml文件的方式保存到程序的程序集目录中。当你在一次修改的时候就会保留上一次的修改结果。代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace TestXml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void createXml()
{
XmlDocument xmldoc = new XmlDocument();
//加入XML的声明段落
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
XmlElement xmlelem = xmldoc.CreateElement("", "Time", "");
xmlelem.InnerText = "20";
xmldoc.AppendChild(xmlelem);
xmldoc.Save(Directory.CreateDirectory(Directory.GetCurrentDirectory()) + "//t.xml");
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void createXml()
{
XmlDocument xmldoc = new XmlDocument();
//加入XML的声明段落
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
XmlElement xmlelem = xmldoc.CreateElement("", "Time", "");
xmlelem.InnerText = "20";
xmldoc.AppendChild(xmlelem);
xmldoc.Save(Directory.CreateDirectory(Directory.GetCurrentDirectory()) + "//t.xml");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Application.StartupPath + "//t.xml");
XmlNode node = xmldoc.SelectSingleNode("Time");
this.textBox1.Text = node.InnerText;
}
catch
{
createXml();
}
}
string t = Directory.GetCurrentDirectory();
private void button2_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Application.StartupPath + "//t.xml");
XmlNode node = xmldoc.SelectSingleNode("Time");
node.InnerText = this.textBox1.Text;
xmldoc.Save(Directory.GetCurrentDirectory() + "//t.xml");
}
catch
{
this.createXml();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Application.StartupPath + "//t.xml");
XmlNode node = xmldoc.SelectSingleNode("Time");
this.textBox1.Text = node.InnerText;
}
catch
{
createXml();
}
}
string t = Directory.GetCurrentDirectory();
private void button2_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Application.StartupPath + "//t.xml");
XmlNode node = xmldoc.SelectSingleNode("Time");
node.InnerText = this.textBox1.Text;
xmldoc.Save(Directory.GetCurrentDirectory() + "//t.xml");
}
catch
{
this.createXml();
}
}
}
}