将列属性保存到XML

时间:2017-11-02 10:58:09

标签: xml vb.net datagridview

所以我成功地使用XML文件来保存和加载来自我的datagridview的数据。但是当我加载文件时,我丢失了datagridview中的列属性,因为我使用的是.columns.clear。有谁知道如何保留我的列属性/将属性保存到XML文件?是否可以在同一XML文件中保存其他文本框和datagridview数据?我的代码如下:

Public Class Dimensioneringsapp
'datasets
Dim DataRekenTab As DataSet

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    'save dataset
    DataRekenTab = CreateDataset(Rekenwaardes)
    Rekenwaardes.DataSource = DataRekenTab.Tables("TestSave")

End Sub

 'load
Private Sub OpenProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenProjectToolStripMenuItem.Click
    LoadFromXMLfile("C:\SaveTest.xml")
End Sub

'save
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
    AddDataSet(DataRekenTab, Rekenwaardes)
    SaveToXMLFile("C:\SaveTest.xml", DataRekenTab)
End Sub

Private Function CreateDataset(g As DataGridView) As DataSet
    Dim dataset1 As New DataSet("SaveTest")
    Dim table1 As New DataTable("SaveTest")

    'counter
    Dim i As Integer = 0

    Do While i < g.ColumnCount
        table1.Columns.Add(g.Columns(i).HeaderText)

        i = i + 1
    Loop

    dataset1.Tables.Add(table1)

    Return dataset1
End Function

Private Sub AddDataSet(d As DataSet, g As DataGridView)

    'counter voor doorlopen tabel
    Dim i As Integer = 0
    Do While i < g.RowCount
        d.Tables("SaveTest").Rows.Add(g(0, i).Value, g(1, i).Value, g(2, i).Value, g(3, i).Value, g(4, i).Value, g(5, i).Value)
        i = i + 1
    Loop

End Sub

Private Sub SaveToXMLFile(filename As String, d As DataSet)
    Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
    Dim writer As TextWriter = New StreamWriter(filename)
    ser.Serialize(writer, d)
    writer.Close()
End Sub

Private Sub LoadFromXMLfile(filename As String)
    If System.IO.File.Exists(filename) Then
        Dim xmlSerializer As XmlSerializer = New XmlSerializer(DataRekenTab.GetType)
        Dim readStream As FileStream = New FileStream(filename, FileMode.Open)

        Rekenwaardes.Columns.Clear()

        DataRekenTab = CType(xmlSerializer.Deserialize(readStream), DataSet)
        readStream.Close()
        Rekenwaardes.DataSource = DataRekenTab.Tables("SaveTest")
    Else
        MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
    End If
End Sub

End class

新代码:

Private Sub SaveToXMLFile(filename As String, d As DataSet)
    d.WriteXml(filename, XmlWriteMode.WriteSchema)
    Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
    Dim writer As TextWriter = New StreamWriter(filename)
    ser.Serialize(writer, d)
    writer.Close()
End Sub


Private Sub LoadFromXMLfile(filename As String)
    If System.IO.File.Exists(filename) Then
        Dim xmlSerializer As XmlSerializer = New XmlSerializer(DataRekenTab.GetType)
        Dim readStream As FileStream = New FileStream(filename, FileMode.Open)

        Rekenwaardes.Columns.Clear()

        DataRekenTab = CType(xmlSerializer.Deserialize(readStream), DataSet)
        readStream.Close()
        DataRekenTab.ReadXml(filename, XmlReadMode.ReadSchema)
        Rekenwaardes.DataSource = DataRekenTab.Tables("SaveTest")
    Else
        MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

使用:

d.WriteXml(filename, XmlWriteMode.WriteSchema)

以便架构将包含在XML文件中。并将数据与架构一起加载

d.ReadXml(filename, XmlReadMode.ReadSchema)