【问题标题】:Save and load multiple values with title使用标题保存和加载多个值
【发布时间】:2013-06-13 18:05:36
【问题描述】:

我正在使用 Visual Studio Express 2012。

我想让我的程序以以下格式仅将数据保存和加载到一个附加数据文件(txt、csv、dll 或其他文件)中(只是一个示例,不必完全是这个,它的只是为了给你看)。

[Title1]
Value1
Value2
Value3
Value4

[Title2]
Value1
Value2
Value3
Value4

程序是这样构建的: 加载表单后,Combobox1 将填充括号“[]”中的所有标题(Title1、Title2 等)

如果您选择一个,则 4 个标签将被值填充。

Label1 = Value1 | Label2 = Value2 | Label3 = Value3 | Label4 = Value4

【问题讨论】:

    标签: vb.net save load external


    【解决方案1】:

    这是一个使用类来保存标题/值和 XmlSerializer 来读取/写入 XML 文件的示例。类的实例保存在一个列表中。

    代码如下:

    Public Class Form1
    
        <Serializable> _
        Public Class TitleGroup
    
            Public Title As String
            Public Value1 As String
            Public Value2 As String
            Public Value3 As String
            Public Value4 As String
    
            Public Overrides Function ToString() As String
                Return Title
            End Function
    
        End Class
    
        Private TitleGroups As New List(Of TitleGroup)
        Private DataFile As String = System.IO.Path.Combine(Application.StartupPath, "Data.xml")
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadXmlData()
        End Sub
    
        Private Sub LoadXmlData()
            If System.IO.File.Exists(DataFile) Then
                Try
                    Dim xml As New System.Xml.Serialization.XmlSerializer(TitleGroups.GetType)
                    Using fs As New System.IO.FileStream(DataFile, IO.FileMode.Open, IO.FileAccess.Read)
                        TitleGroups = DirectCast(xml.Deserialize(fs), List(Of TitleGroup))
                    End Using
                    ComboBox1.DataSource = Nothing
                    ComboBox1.DataSource = TitleGroups
                Catch ex As Exception
                    MessageBox.Show(ex.ToString, "Error Loading XML Data")
                End Try
            End If
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex <> -1 Then
                Dim TG As TitleGroup = ComboBox1.SelectedItem
                Label1.Text = TG.Value1
                Label2.Text = TG.Value2
                Label3.Text = TG.Value3
                Label4.Text = TG.Value4
            End If
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            SaveXmlData()
        End Sub
    
        Private Sub SaveXmlData()
            Try
                Dim xml As New System.Xml.Serialization.XmlSerializer(TitleGroups.GetType)
                Using fs As New System.IO.FileStream(DataFile, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                    xml.Serialize(fs, TitleGroups)
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.ToString, "Error Saving XML Data")
            End Try
        End Sub
    
    End Class
    

    这是一个示例 XML 文件:

    <?xml version="1.0"?>
    <ArrayOfTitleGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <TitleGroup>
        <Title>Pets</Title>
        <Value1>Dog</Value1>
        <Value2>Cat</Value2>
        <Value3>Fish</Value3>
        <Value4>Hamster</Value4>
      </TitleGroup>
      <TitleGroup>
        <Title>Languages</Title>
        <Value1>C#</Value1>
        <Value2>VB.Net</Value2>
        <Value3>Java</Value3>
        <Value4>Objective-C</Value4>
      </TitleGroup>
    </ArrayOfTitleGroup>
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2016-02-14
      • 2014-04-21
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多