本文发表在 rolia.net 枫下论坛为方便加了个根单元(Root)。
Imports System.Linq
Imports System.Data.SqlClient
Public Class Class4
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim i As Integer
Dim Xe As New XElement("Root")
Dim myName As String
Dim myArrayList As New ArrayList
For i = 0 To myDS.Tables(0).Rows.Count - 1
myName = CStr(myDS.Tables(0).Rows(i)("Parent"))
If HasParent(myName, myDS.Tables(0)) = False AndAlso _
Not myArrayList.Contains(myName) Then
myArrayList.Add(myName)
Xe.Add(Recursion(CStr(myDS.Tables(0).Rows(i)("Parent")), myDS.Tables(0)))
End If
Next
MsgBox(Xe.ToString)
End Sub
Private Function HasParent(ByVal name As String, ByVal DT As DataTable) As Boolean
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Child")) = name Then
Return True
End If
Next
Return False
End Function
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As XElement
Dim myParentElement As XElement = _
<ul>
<li>
<%= Name %>
<%= From E In DT Where E("Parent") = Name Select Recursion(E("Child"), DT) %>
</li>
</ul>
Return myParentElement
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
Imports System.Linq
Imports System.Data.SqlClient
Public Class Class4
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim i As Integer
Dim Xe As New XElement("Root")
Dim myName As String
Dim myArrayList As New ArrayList
For i = 0 To myDS.Tables(0).Rows.Count - 1
myName = CStr(myDS.Tables(0).Rows(i)("Parent"))
If HasParent(myName, myDS.Tables(0)) = False AndAlso _
Not myArrayList.Contains(myName) Then
myArrayList.Add(myName)
Xe.Add(Recursion(CStr(myDS.Tables(0).Rows(i)("Parent")), myDS.Tables(0)))
End If
Next
MsgBox(Xe.ToString)
End Sub
Private Function HasParent(ByVal name As String, ByVal DT As DataTable) As Boolean
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Child")) = name Then
Return True
End If
Next
Return False
End Function
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As XElement
Dim myParentElement As XElement = _
<ul>
<li>
<%= Name %>
<%= From E In DT Where E("Parent") = Name Select Recursion(E("Child"), DT) %>
</li>
</ul>
Return myParentElement
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net