Introduction
This is my first article to code project, its been more than 3 months following the articles I felt interesting the way everyone submitting the articles ,I feel the time has come to submit an article which will be helpful to naive developers.I here presenting an article which involves vb coding, xml generation ,using xslt and javascript .
Before wetting our foot with coding I want to give bird's eye of whats this article about.
There was a requirement like showing a tree view of folder structure which is very fickle(mean folder structure is changing), when ever user hits the page he must be viewed with latest folders ,documents etc., present in system(server) should be viewed. To keep up the requirement I used xml which will be generated on the fly when user hits the page and to beautify the front end we used xslt and javascript to follow user navigation in front end.I would explain the objects used with respect to code.
Using the code
Prerequisites for following the code a small pinch of knowledge about javascript,xslt and bit more experience on visual basics.
here I go first with namespaces used in this application.
<br />imports system.xml<br />imports system.xml.xsl<br />imports system.text<br />imports system.io
These are namespaces used for accessing system directory objects and xml objects.
(File Path and other variables naming conventions are not been standardized)
Dim tw As XmlTextWriter
tw = New XmlTextWriter("C:\\test\test12345" & ".xml", _<br /> New System.Text.UTF8Encoding())<br /><br /> tw.Formatting = Formatting.Indented<br /> 'tw.Indentation = 4<br /><br /> ' Write out the header information<br /> tw.WriteStartDocument()'starting the xml with tag name "folders" you can change it to other 'but change correspondingly in xslt file also tw.WriteStartElement("FOLDERS")'starting the xml with tag name "folder" you can change it to other 'but change correspondingly in xslt file also tw.WriteStartElement("FOLDER")<br /> tw.WriteAttributeString("name", "Knowledge Tree")<br /> tw.WriteStartElement("FOLDER")<br /> tw.WriteAttributeString("name", "Certification Material")In the above code declared a object to a class xmltextwriter which accepts a xml file location as argument(constructor) .This object has properties like formatting ,indentation for generating valid xml of version-1 and encoding="utf-8" there by following method 'writestartdocument' which starts xml document,before we go further I will explain when to which method of xmltextwriter starting with WriteElementString it will output with text between the tags(<title>Enter status of bug for search</title>) it accepts arguments two arguments tag name and text to be displayed(tw.WriteElementString("title", "Enter status of bug for search")),of course we didn't use in code it will be helpful,coming to WriteStartElement using which we can starts new tag(<folder>) there must corresponding end element using writeendelement(</folder) this tag pretty trickery, if this tag don't have any child elements(another writestartelement) this will be self ending tag. If it has another writestartelement( before writeendelement) this will become parent tag.
Iam using recursive function for digging the directory.where below function is self explanatory which calls itself until filesysteminfo is not a directory(filesysteminfo.name<>directory)., otherwise(.doc,.txt ) it ends as leaf.
Function fileinfo(ByVal strfilepath As String) As Collection<br /> Dim colinner As New Collection<br />
colinner) is not a
Then coming to IO objects let me elucidate with respect to code,(Sorry for my impatience in following Coding Standards.)
Dim strfileinfopath As String = System.AppDomain.CurrentDomain.BaseDirectory & "docs/"<br /> Dim df As DirectoryInfo<br />
col) Dim strtype As Type = strtest.GetType<br />
testmaterial " Then<br /> filecollection = fileinfo(strfileinnerpath)<br /> End If<br /> tw.WriteEndElement()<br /> Else<br />'coming to else part means not a item in collection(col) is not a 'directory it is of .doc or .pdf or .ppt etc.m<br /> Dim filename As FileInfo<br /> filename = col.Item(tempdoc)<br /> Dim strfull As String = filename.FullName<br /> tw.WriteStartElement("KEY")<br /> tw.WriteAttributeString("name", filename.Name)<br /> tw.WriteAttributeString("Path", strfull)<br /> tw.WriteEndElement()<br /> End If<br /> Next<br /> ' closing all the nodes correspondingly<br /> tw.WriteEndElement()<br /> tw.WriteEndElement()<br /> tw.WriteEndElement()<br /> tw.WriteEndDocument()<br />'diposing all the object of xml<br /> tw.Flush()<br /> tw.Close()<br /> End IfStarting with System.AppDomain.CurrentDomain.BaseDirectory which returns path of base directory of your application as it developed in vs.net in this case returns "C://inetpub/wwwroot/treeview".coming to DirectoryInfo class which returns all the directory information about the path passed as argument.
in this for loop "fsi" is object of filesysteminfo class ,where getfilesysteminfos is a method of derectoryinfo which returns files or directory information for each of type filesysteminfo
For Each fsi In df.GetFileSystemInfos<br /> Dim shi = fsi<br /> col.Add(fsi)<br /> Next
Here we are calling
fileinfo function because it is directory(I mean it is file which may contain sub folders in which contain documents or sub folders)
filecollection = fileinfo(strfileinnerpath)
Coming to crux path applying xslt to xml
Dim doc As New XmlDocument()<br /><br /> doc.Load(Server.MapPath("test12345.xml"))<br /><br /> Dim t As New XslTransform()<br /> t.Load(Server.MapPath("images/XSLTFile1.xslt"))<br /> Dim sb As New StringBuilder()<br /> Dim writer As TextWriter<br /> writer = New StringWriter(sb)<br /><br /><br /> t.Transform(doc, Nothing, writer)<br /> Label1.Text = sb.ToString()<br />}
In the above block of code loading the xml document using server.mappath and applying xslt to xml using
XslTransform class.
I am ending this article with giving code(this code is need more parsed to xml ) how to read ,you can skip this if know how to read xml document or having knowledge about xmltextreader class.
Dim oRead As XmlTextReader<br />Dim cou As Integer = 1<br /><br /> Try<br /> oRead = New XmlTextReader("C:\inetpub\wwwroot\test\test12345.xml")<br /> Do While oRead.Read<br /> oRead.MoveToContent()<br /> If oRead.Name.Equals("KEY") Then<br /><br /> If oRead.GetAttribute("name").ToString = lsStatus Then<br /> Dim strpath As String = oRead.GetAttribute("path")<br /> Dim stractpath = Split(strpath, "test\")<br /> Dim stractpath1 = stractpath(1)<br /> I here conclude this article with the whole code contains just three objects
1.directoryinfo
2.xmltextwriter
3.xsltransform
4.xmltextreader
I mean to say nothing to daunt just playing around those four objects and calling a function recursively.
ABOUT DOWNLOAD.
application presented in zip file is compatible to v1.1 and visual studio 2003.