2009-01-29 84 views
3

下面是數據的我的對象將需要公開的結構(該數據是不是真的以XML格式存儲,這只是爲了說明佈局的最簡單的方法):我應該使用哪種.NET數據結構?

<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。

回答

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語法)。一定要使用屬性與公衆會員...

+0

+1,加上部門的通用集合 – 2009-01-29 17:11:10

2
  • 一個部門對象
  • Employee對象
  • 的employeeCollection對象。 (可選,你可以使用List(Employee))

將它們全部標記爲可序列化,然後你可以將它們序列化爲你喜歡的任何格式。

6

一個Department類(帶ID和Name),它包含一個Employee(ID和Name)對象的集合。

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