如何将dataGridView中的所有数据保存到XML。 现在只保存文本框。 非常感谢你
大家好,如何将dataGridView中的所有数据保存到XML。 现在只保存文本框。 非常感谢你
public partial class SaveToXML : Form
{
private XmlDocument doc;
private XmlElement root;
public string PATH = @"H:\\Data.xml";
public SaveToXML()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[n].Cells[1].Value = textBox2.Text;
dataGridView1.Rows[n].Cells[2].Value = textBox3.Text;
dataGridView1.Rows[n].Cells[3].Value = textBox4.Text;
dataGridView1.Rows[n].Cells[4].Value = textBox5.Text;
}
private void button4_Click(object sender, EventArgs e)
{
XmlWriter xmlWriter = XmlWriter.Create(PATH);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Event_Information");
xmlWriter.WriteAttributeString("Data_Set_Type", "REPLACE");
xmlWriter.WriteStartElement("Service");
xmlWriter.WriteAttributeString("Name", "TV");
xmlWriter.WriteStartElement("Event");
xmlWriter.WriteAttributeString("Start", textBox1.Text + " " + textBox2.Text);
xmlWriter.WriteAttributeString("Duration", textBox3.Text);
xmlWriter.WriteStartElement("short-event-descriptor");
xmlWriter.WriteAttributeString("Language", "deu");
xmlWriter.WriteStartElement("Name");
xmlWriter.WriteString(textBox4.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("extended-event-descriptor");
xmlWriter.WriteAttributeString("Language", "deu");
xmlWriter.WriteStartElement("Text");
xmlWriter.WriteString(textBox5.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
答案 0 :(得分:1)
试试这个:
private void button4_Click(object sender, EventArgs e)
{
//Store
DataSet ds = (DataSet) dataGridView1.DataSource;
ds.WriteXml("path");
//Load
string path = @"path";
DataSet ds = new DataSet();
ds.ReadXml(path);
dataGridView1.DataSource = ds;
}