469,609 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

build a xml tree recursively

Hi,

I have the following records stored in the database :

TableName: Folder { FolderID, FolderName, TopLevel}

TableName: FolderRelationship { ParentID, ChildID}

This describes a folder tree structure. Note that only one record has
TopLevel set to 1, this is the top folder in the structure.

I get the records in two datatables. Then I would like to build the
following xml based on the records :

<node folderid=1 foldername="top folder">
<node folderid=2 foldername "firstchild" />
<node folderid=3 foldername "secondchild">
<node folderid=4 foldername="thirdchild" />
</node>
</node>

which would correspond to the following tree:

top folder
|-firstchild
|-secondchild
|-thirdchild

How can I build such a tree recursively ?

Thank you

Jan 24 '07 #1
2 2105
graphicsxp wrote:
TableName: Folder { FolderID, FolderName, TopLevel}

TableName: FolderRelationship { ParentID, ChildID}
This describes a folder tree structure. Note that only one record has
TopLevel set to 1, this is the top folder in the structure.
How can I build such a tree recursively ?
Locate the (one and only) root "node", then process it and its child
"nodes" using a recursive function, something like this (air code)...

Sub SaveTree( _
folderId as Integer? _
, folderName as String
, sw as StreamWriter _
)
' Save the current Node
sw.WriteLine( "<node" _
& " folderid='" & folderId & "'" _
& " foldername='" & folderName & "'" _
& ">" )

' Now locate any child "nodes"
Dim ds as DataSet = GetChildrenOf( folderId )
If ds.Tables(0).Rows 0 Then
' Loop through and save child nodes
For Each dr as DataRow In ds.Tables(0).Rows
SaveTree( CInt?(dr.Item("ChildId")) _
, dr.Item("Name").ToString() _
, sw )
Next
End If

sw.WriteLine( "</node>" )
End Sub

.... then ...

Dim sw as New streamwriter( "file" )
SaveTree( rootId, rootName, sw )

HTH,
Phill W.
Jan 24 '07 #2
On Jan 24, 4:35 am, "graphicsxp" <samuelberthe...@googlemail.com>
wrote:
>
TableName: Folder { FolderID, FolderName, TopLevel}
TableName: FolderRelationship { ParentID, ChildID}
Any particular reason you have it in two tables? You could do it with
only a single table:

TableName: Folder {FolderId, FolderName, ParentId}

Then, the top level folder will be identified by having a NULL value
for ParentId. And a folder can find its child folders by looking for
all folders with a parentId equal to its own id.
top folder
|-firstchild
|-secondchild
|-thirdchild
Can a folder have only one child or multiple children?

Chris

Jan 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sims | last post: by
8 posts views Thread by ted | last post: by
25 posts views Thread by prabhat143 | last post: by
10 posts views Thread by Dan Nash | last post: by
3 posts views Thread by Yuri CHUANG | last post: by
3 posts views Thread by FARAECHILIBRU | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.