hi
i have a compelete solution project for importing and exporting xml files to datagridview and vise versa.
i hope my project will be useful for you.
follow these steps:
1- create new windows forms application project
2- drag a datagridview and rename it to: dgv
3- drag 3 buttons: and change their names to btnRead, btnWrite, btnClear
and their text as "Read XML","Write XML","Clear"
4- double click on the form1 and press control+A to select all text and replace it by the codes below:
Public Class Form1
Dim dt As DataTable
Dim fs As IO.Stream
Dim ss As IO.Stream
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim sd As New OpenFileDialog
sd.Filter = "XML ducuments(*.XML)|*.xml"
sd.DefaultExt = "xml"
If sd.ShowDialog = DialogResult.OK Then
fs = sd.OpenFile
Change_Datatable()
If IO.File.Exists(IO.Path.ChangeExtension(sd.FileName, ".sch")) Then
ss = New IO.StreamReader(IO.Path.ChangeExtension(sd.FileName, ".sch")).BaseStream
dt.ReadXmlSchema(ss)
ss.Close()
Else
MsgBox("فایل حاوی تنظیمات دیتابیسی(XML-Schema) برای XML انتخاب شده یافت نشد.", MsgBoxStyle.MsgBoxRtlReading Or MsgBoxStyle.MsgBoxRight)
Return
End If
dt.ReadXml(fs)
fs.Close()
End If
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim sd As New SaveFileDialog
sd.Filter = "XML ducuments(*.XML)|*.xml"
sd.DefaultExt = "xml"
If sd.ShowDialog = DialogResult.OK Then
fs = sd.OpenFile
ss = New IO.StreamWriter(IO.Path.ChangeExtension(sd.FileName, ".sch")).BaseStream
dt.WriteXmlSchema(ss)
dt.WriteXml(fs)
fs.Close()
ss.Close()
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
dt.Clear()
End Sub
Public Sub Change_Datatable()
dt = New DataTable
dt.TableName = "Tables(0)"
dgv.DataSource = dt
End Sub
End Class