Writing from DataGridView to XML

rodan

New member
Joined
Dec 19, 2012
Messages
1
Programming Experience
Beginner
I can successfully use the code below to import data from an XML file into a DataGridView, but now I need to be able to write the same data back out to XML.

Dim xmlDatadoc As XmlDataDocument = New XmlDataDocument()
xmlDatadoc.DataSet.ReadXml(OpenFD.FileName)
Dim ds As DataSet = New DataSet("Version DataSet")
ds = xmlDatadoc.DataSetDataGridView1.DataSource = ds.DefaultViewManager
DataGridView1.DataMember = "Version"
Can I use something like:

xmlDatadoc.DataSet.Writexml(OpenFD.FileName)
I'm thinking there's some sort of reverse way to do this, but I'm completely stuck. Basically, I need to store records of file versions (VerID = 1, 2, 3...20) into a DataGrid, edit it, and then save it back in the same format. Let me know if you need more info, I'm a beginner here.
 
Hi,

You can easily Read and Write an XML file directly from a DataSet object. Have a look here:-

VB.NET:
Dim DS As New DataSet("YourDataSetName")
 
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  DS.ReadXml("d:\temp\YourFile.xml")
  DataGridView1.DataSource = DS.Tables(0)
End Sub

You can then edit the DataGridView control as you need and then to save, just call:-

VB.NET:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  DS.WriteXml("d:\temp\YourFile.xml")
  MsgBox("Saved")
End Sub

Hope that helps.

Cheers,

Ian
 
Back
Top