Hello !
I'm trying to make a DNNTree for displaying folders on my hard disk.
I can't make it works for two reasons :
- The XML seemed to be mal formed,
- I don't really know how to do with the WriteEndElement, i may use a pile but ... how to ?
Here my code, and at the end, the exception i get.
Sébastien
----------------------------------------------CODE----------------------------------------------
Public _myId As Integer
Public Property myId() As Integer
Get
If _myId <> Nothing Then
Return _myId
Else
Return 0
End If
End Get
Set(ByVal value As Integer)
_myId = value
End Set
End Property
Public Sub buildXMLTreeSubLevels(ByVal _path As String, ByVal monXmlWriter As XmlWriter)
Dim folders As String()
folders = Directory.GetDirectories(_path)
Dim folder As String
Dim objFile As IO.FileInfo
Dim objFolder As DirectoryInfo
For Each folder In folders
objFolder = New System.IO.DirectoryInfo(folder)
monXmlWriter.WriteStartElement("n")
myId = myId + 1
monXmlWriter.WriteAttributeString("id", myId)
monXmlWriter.WriteAttributeString("txt", objFolder.Name)
monXmlWriter.WriteAttributeString("img", "../../../Images/folder.gif")
monXmlWriter.WriteAttributeString("ca", 1)
Dim subFolders As String() = Directory.GetDirectories(folder)
If Not subFolders.Length <> Nothing Then ' The folder don't have any sub folder
If Not objFolder.GetFiles("*.txt").Length <> Nothing Then ' The folder don't have a txt file
monXmlWriter.WriteAttributeString("hasNodes", "0")
Else
monXmlWriter.WriteAttributeString("hasNodes", "1")
End If
Else
monXmlWriter.WriteAttributeString("hasNodes", "1")
buildXMLTreeSubLevels(folder, monXmlWriter)
End If
' Look for TXT files
For Each objFile In objFolder.GetFiles("*.txt")
Dim strLocalNFOFile As String = PortalSettings.HomeDirectoryMapPath & "\LocalesTXT\" & objFolder.Name & objFile.Name
If Not File.Exists(strLocalNFOFile) Then
File.Copy(objFile.FullName, strLocalNFOFile)
End If
' TODO: Transform to non-static method using something like
'Dim objFileController As New DotNetNuke.Services.FileSystem.FileController
'Dim fileId As Integer = objFileController.ConvertFilePathToFileId(objFile.FullName, PortalId)
'Dim objFileInfo As DotNetNuke.Services.FileSystem.FileInfo = objFileController.GetFileById(fileId, PortalId)
monXmlWriter.WriteStartElement("n")
myId = myId + 1
monXmlWriter.WriteAttributeString("id", myId)
monXmlWriter.WriteAttributeString("txt", objFile.Name)
monXmlWriter.WriteAttributeString("img", "../../../Images/file.gif")
monXmlWriter.WriteAttributeString("ca", 3)
monXmlWriter.WriteAttributeString("url", "http://hard-coded-IP/Portals/0/LocalesTXT/" & objFolder.Name & objFile.Name) ' TODO: Transform to non-static method
monXmlWriter.WriteAttributeString("tar", "_blank")
monXmlWriter.WriteEndElement()
Next
monXmlWriter.WriteEndElement()
Next
monXmlWriter.WriteEndElement()
End Sub
Public Sub buildXMLTree() 'First method called
' ----------- INIT FOLDERS
Dim folders As String()
folders = Directory.GetDirectories("Z:\SharedData\")' TODO: Transform to non-static method from module settings
' ----------- INIT XML BUILDER
Dim portalsXmlFilesPath As String = PortalSettings.HomeDirectoryMapPath & "DNNTreeXMLs"
If Not Directory.Exists(portalsXmlFilesPath) Then
Directory.CreateDirectory(portalsXmlFilesPath)
End If
Dim monFileName As String = portalsXmlFilesPath & "\" & "DNNTree_" & Me.ModuleId & ".xml"
Dim monXmlWriter As New XmlTextWriter(monFileName, Text.Encoding.UTF8)
monXmlWriter.Formatting = Formatting.Indented
monXmlWriter.WriteStartElement("root")
monXmlWriter.WriteAttributeString("id", "DNNTree") ' TODO: Transform to non-static method
Dim folder As String
Dim objFile As IO.FileInfo
Dim objFolder As DirectoryInfo
Dim count As Integer = 0
' Look for sub folders
For Each folder In folders
objFolder = New System.IO.DirectoryInfo(folder)
monXmlWriter.WriteStartElement("n")
If count = 0 Then ' IDs starts from 0
myId = 0
count = 1
Else
myId = myId + 1
End If
monXmlWriter.WriteAttributeString("id", myId)
monXmlWriter.WriteAttributeString("txt", objFolder.Name)
monXmlWriter.WriteAttributeString("img", "../../../Images/folder.gif")
monXmlWriter.WriteAttributeString("ca", 1)
Dim subFolders As String() = Directory.GetDirectories(folder)
If Not subFolders.Length <> Nothing Then
If Not objFolder.GetFiles("*.txt").Length <> Nothing Then
monXmlWriter.WriteAttributeString("hasNodes", "0")
Else
monXmlWriter.WriteAttributeString("hasNodes", "1")
End If
Else
monXmlWriter.WriteAttributeString("hasNodes", "1")
End If
' Look for TXT files
For Each objFile In objFolder.GetFiles("*.txt")
Dim strLocalNFOFile As String = PortalSettings.HomeDirectoryMapPath & "LocalesTXT\" & objFolder.Name & objFile.Name
If Not File.Exists(strLocalNFOFile) Then
File.Copy(objFile.FullName, strLocalNFOFile)
End If
' TODO: Transform to non-static method using something like
'Dim objFileController As New DotNetNuke.Services.FileSystem.FileController
'Dim fileId As Integer = objFileController.ConvertFilePathToFileId(objFile.FullName, PortalId)
'Dim objFileInfo As DotNetNuke.Services.FileSystem.FileInfo = objFileController.GetFileById(fileId, PortalId)
monXmlWriter.WriteStartElement("n")
myId = myId + 1
monXmlWriter.WriteAttributeString("id", myId)
monXmlWriter.WriteAttributeString("txt", objFile.Name)
monXmlWriter.WriteAttributeString("img", "../../../Images/file.gif")
monXmlWriter.WriteAttributeString("ca", 3)
monXmlWriter.WriteAttributeString("url", "http://hard-coded-IP/Portals/0/LocalesTXT/" & objFolder.Name & objFile.Name)
monXmlWriter.WriteAttributeString("tar", "_blank")
monXmlWriter.WriteEndElement()
Next
buildXMLTreeSubLevels(folder, monXmlWriter) ' recursive method
monXmlWriter.WriteEndElement()
Next
monXmlWriter.WriteEndElement()
monXmlWriter.Flush()
monXmlWriter.Close()
DNNTree.LoadXml(monFileName)
End Sub
---------------------------------------------------------------------------------------------------
------------------------------------------------EXC----------------------------------------------
DotNetNuke.Services.Exceptions.ModuleLoadException: Data at the root level is invalid. Line 1, position 1.
--->
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
at DotNetNuke.UI.WebControls.XmlCollectionBase..ctor(String strXML, String strXSLFile)
at DotNetNuke.UI.WebControls.DNNNodeCollection..ctor(String strXML, String strXSLFile)
at DotNetNuke.UI.WebControls.TreeNodeCollection..ctor(String strXML, String strXSLFile, DnnTree objControl)
at DotNetNuke.UI.WebControls.DnnTree.LoadXml(String strXml)
at Aricie.Modules.TestSharedDataManager.ViewTestSharedDataManager.buildXMLTree()
in Z:\Sites Web\Developpement\440\Website\DesktopModules\TestSharedDataManager\ViewTestSharedDataManager.ascx.vb:line 340
at Aricie.Modules.TestSharedDataManager.ViewTestSharedDataManager.Page_Load(Object sender, EventArgs e)
in Z:\Sites Web\Developpement\440\Website\DesktopModules\TestSharedDataManager\ViewTestSharedDataManager.ascx.vb:line 394
--- End of inner exception stack trace ---
---------------------------------------------------------------------------------------------------
------------------------------------------------EXC#2----------------------------------------------
DotNetNuke.Services.Exceptions.ModuleLoadException:
There was no XML start tag open.
--->
System.InvalidOperationException: There was no XML start tag open.
at System.Xml.XmlTextWriter.InternalWriteEndElement(Boolean longFormat)
at System.Xml.XmlTextWriter.WriteEndElement()
at Aricie.Modules.TestSharedDataManager.ViewTestSharedDataManager.buildXMLTreeSubLevels(String _path, XmlWriter monXmlWriter)
in Z:\Sites Web\Developpement\440\Website\DesktopModules\TestSharedDataManager\ViewTestSharedDataManager.ascx.vb:line 237
at Aricie.Modules.TestSharedDataManager.ViewTestSharedDataManager.buildXMLTree()
in Z:\Sites Web\Developpement\440\Website\DesktopModules\TestSharedDataManager\ViewTestSharedDataManager.ascx.vb:line 323
at Aricie.Modules.TestSharedDataManager.ViewTestSharedDataManager.Page_Load(Object sender, EventArgs e)
in Z:\Sites Web\Developpement\440\Website\DesktopModules\TestSharedDataManager\ViewTestSharedDataManager.ascx.vb:line 386
--- End of inner exception stack trace ---
---------------------------------------------------------------------------------------------------
The French Language pack for fr-FR is available at www.dnn-fr.org
|