با سلام بنده یک دیتاگرید ایجاد کردم که اطلاعاتی رو با opendialog باز میکنه و به دیتا گرید اضافه میکنه و اطلاعات و باید به شکل xml ذخیره کنه که متاسفانه این اتفاق نمیفته، ممنون میشم راهنمایی بفرمایید، این هم کدهای برنامه بنده:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Janus.Windows.UI.Dock;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.IO;
namespace Mgdk
{
public partial class Form1 : Form
{
private string strFileName;
public Form1()
{
InitializeComponent();
}
public void splashstart()
{
Application.Run(new Form2());
}
private void buttonX4_Click(object sender, EventArgs e)
{
String s="";
openFileDialog1.Filter = "TXT Files|*.txt";
openFileDialog1.FileName = String.Empty;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Stream fs = openFileDialog1.OpenFile();
StreamReader reader = new StreamReader(fs);
while (!reader.EndOfStream)
{
s+= reader.ReadLine();
}
string[] str = s.Split('*','@','+',':',';','&','!');
dataGridViewX1.Rows.Add(str[1],str[2],str[3],str[4],str[5],str[6]);
reader.Close();
}
}
private DataTable GetDataTableFromDGV(DataGridView dataGridViewX1)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dataGridViewX1.Columns)
{
if (column.Visible)
dt.Columns.Add();
}
object[] cellValues = new object[dataGridViewX1.Columns.Count];
foreach (DataGridViewRow row in dataGridViewX1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}
private void buttonX3_Click_1(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Xml files (*.xml)|*.xml";
saveDialog.InitialDirectory = "d:\\";
saveDialog.FileName = "XML File";
saveDialog.Title = "XML Export";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
DataTable dT = GetDataTableFromDGV(dataGridViewX1);
DataSet dS = new DataSet();
dS.Tables.Add(dT);
dS.WriteXml(System.IO.File.OpenWrite(saveDialog.FileName));
}
private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}