【文件属性】:
文件名称:C# 读写XML实例
文件大小:13KB
文件格式:RAR
更新时间:2012-05-22 15:45:34
C#, 读写XML,实例
C# 读写XML文件,VS2005环境
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace XmlApp
{
public partial class Form1 : Form
{
private string filePath;//文件名称
DataSet dsAuthors = new DataSet("authors"); //数据表
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
filePath = Application.StartupPath + "\\作者.xml";
dsAuthors.Clear();
//显示数据
dsAuthors.ReadXml(filePath);
dataGrid1.DataSource = dsAuthors;
dataGrid1.DataMember = "authors";
dataGrid1.CaptionText = dataGrid1.DataMember;
MessageBox.Show(this, "Xml文件的数据已经加载。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button2_Click(object sender, EventArgs e)
{
dsAuthors.WriteXml(filePath);//保存到Xml文件
MessageBox.Show(this, "数据已经保存到Xml文件。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;//有DataGrid控件时,必须以编程方式设置才有效(应该是VS2005的BUG)
}
}
}
【文件预览】:
读写Xml文件
----XmlApp()
--------Form1.cs(1KB)
--------bin()
--------Properties()
--------Program.cs(465B)
--------Form1.Designer.cs(3KB)
--------XmlApp.csproj(3KB)
--------Form1.resx(6KB)
----XmlApp.sln(907B)
----XmlApp.suo(13KB)