gpt4 book ai didi

vb.net - 如何使用 vb.net 创建输出将采用分层结构的父子集合

转载 作者:行者123 更新时间:2023-12-04 19:35:13 25 4
gpt4 key购买 nike

我想在 VB.NET 中创建一个层次结构,如下所示

<Parent>
<Child1><Child1 />
<Child2>
<Subchild1 />
<Subchild2 />
<Child2 />
</Parent>

我为 parent 创建了实体类和集合类, child1 , child2 , subchild1subchild2 .我需要将父集合类实例传递给 XML 序列化器类以生成上面给出的分层节点结构。我不知道该怎么做。请给我一个 sample 。

实体:
Public class Parent
Public property FirstName as string
Public property LastName as string
End Class

Public Class Child1
Public property Color as string
End class

Public Class Child2
Public property Color as string
End class

Public Class SubChild1
Public property FirstName as string
End Class

Public Class SubChild2
Public property FirstName as string
End Class

Collection Class:
Public class ParentS
Public Function Add(objrow as Parent, byref skey as object) as Parent

我是否需要将子类作为属性添加到父类?如何做到这一点并创建上面给出的结构。请帮忙。谢谢。

最佳答案

你可以这样:

Public Class Node

Public Property FirstName As String
Public Property LastName As String

Private _childNodes As New List(Of Node)

Public Property ChildNodes As List(Of Node)
Get
Return _childNodes
End Get
Set
_childNodes = value
End
End Property

End Class
用法
Dim parent As New Node
parent.FirstName = "John"
parent.LastName = "Doe"

Dim child_1 As New Node()
child_1.FirstName = "Jane"
child_1.LastName = "Doe"
parent.ChildNodes.Add(child_1)
更新
Public Class Employee

Public Property FirstName As String
Public Property LastName As String

End Class

Public Class Department

Private _employees As New List(Of Employee)
Private _subDepartments As New List(Of Department)

Public Property SubDepartments As List(Of Department)
Get
Return _subDepartments
End Get
Set
_subDepartments = value
End
End Property

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

End Class
用法
Dim dept As New Department
dept.Name = "Accounting"

Dim subDept1 As New Department()
subDept1.Name = "Audit"

dept.SubDepartments.Add(subDept1)

Dim employee1 As New Employee()
employee1.FirstName = "John"
employee1.LastName = "Doe"

dept.Employees.Add(employee1)
希望有帮助!这可以进一步重构,但这应该有效。

关于vb.net - 如何使用 vb.net 创建输出将采用分层结构的父子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com