Darryl Smith @ Radioactive Networks: Programming Notes - Directory to XML recursive

Friday, July 02, 2004

Programming Notes - Directory to XML recursive

Converting a Directory Structure to XML in VB.NET



There is an article on Builder.Com.Com showing how to convert a directory structure to XML. Unfortunately it is not a recursive function, but how hard can that be? OK, well I know how hard it can be, but it should not be too hard. We will see.

There were some issues, but I got it to work. It is a start at least.

Public Function DirectoryStart(ByVal path As String) As String
Dim sw As New StringWriter
Dim xmlwriter As New XmlTextWriter(sw)
xmlwriter.WriteStartDocument()
DirectoryRecurse(path, xmlwriter)
xmlwriter.WriteEndDocument()
xmlwriter.Close()
Return sw.ToString
End Function

Public Sub DirectoryRecurse(ByVal path As String, ByVal xmlw As XmlWriter)
Dim filename As String ' , path As String
Dim dir As New DirectoryInfo(path)
Dim xtw As XmlTextWriter
With xmlw
.WriteComment(" Content of the " & Chr(34) & path & Chr(34) & " folder ")
.WriteStartElement("folders")
.WriteAttributeString("path", path)
.WriteAttributeString("count", dir.GetDirectories().Length.ToString())
For Each d As DirectoryInfo In dir.GetDirectories()
.WriteStartElement("folder")
.WriteAttributeString("name", d.Name)
.WriteAttributeString("accessed", d.LastAccessTime.ToString())
.WriteString("Content of " + d.Name)
.WriteEndElement()
DirectoryRecurse(d.FullName, xmlw)
Next
.WriteEndElement()
End With
End Sub

0 Comments:

Post a Comment

<< Home