我应该使用哪种.NET数据结构?

时间:2009-01-29 17:02:47

标签: .net vb.net data-structures

这是我的对象需要公开的数据的结构(数据实际上并不存储在XML中,它只是说明布局的最简单方法):

<Department id="Accounting">
  <Employee id="1234">Joe Jones</Employee>
  <Employee id="5678">Steve Smith</Employee>
</Department>
<Department id="Marketing">
  <Employee id="3223">Kate Connors</Employee>
  <Employee id="3218">Noble Washington</Employee>
  <Employee id="3233">James Thomas</Employee>
</Department>

当我对数据进行反序列化时,我应该如何在对象的属性方面公开它?如果它只是Department和EmployeeID,我想我会使用字典。但我也需要关联EmployeeName。

4 个答案:

答案 0 :(得分:7)

Class Department
   Public Id As Integer
   Public Employees As List(Of Employee)
End Class

Class Employee
   Public Id As Integer
   Public Name As String
End Class

类似的东西(不记得我的VB语法)。请务必使用“属性与公共成员...”

答案 1 :(得分:6)

Department类(带ID和名称),包含Employee(ID和Name)对象的集合。

答案 2 :(得分:4)

Public Class Employee

    Private _id As Integer
    Public Property EmployeeID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property


End Class

Public Class Department

    Private _department As String
    Public Property Department() As String
        Get
            Return _department
        End Get
        Set(ByVal value As String)
            _department = value
        End Set
    End Property

    Private _employees As List(Of Employee)
    Public Property Employees() As List(Of Employee)
        Get
            Return _employees
        End Get
        Set(ByVal value As List(Of Employee))
            _employees = value
        End Set
    End Property

End Class

答案 3 :(得分:2)

  • 部门对象
  • 员工对象
  • employeeCollection对象。 (可选,您可以使用List(员工))

将它们全部标记为可序列化,然后您可以(de)将它们序列化为您喜欢的任何格式。