Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Writing DirectoryInfo() to XML File

Writing DirectoryInfo() to XML File

Scheduled Pinned Locked Moved Visual Basic
xmlhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cwayman
    wrote on last edited by
    #1

    Hey, Using the FolderBrowserDialog i select a folder. From here i want to recurse through this folder and add it (and all its subfolders and files) to an XML file so it can then populate a treeeview. I am having trouble recursing the Folders and files in to the correct xml nodes during the recursion. I am using structure: And for Sub Folders Any help or tip or links would be very helpful thanks for your time Chris

    J 1 Reply Last reply
    0
    • C cwayman

      Hey, Using the FolderBrowserDialog i select a folder. From here i want to recurse through this folder and add it (and all its subfolders and files) to an XML file so it can then populate a treeeview. I am having trouble recursing the Folders and files in to the correct xml nodes during the recursion. I am using structure: And for Sub Folders Any help or tip or links would be very helpful thanks for your time Chris

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      hi, This is a particularly easy problem to solve as a small recursive function will do all the work for you. The best way I could think to do this was to derive a class from XmlTextWriter - in my case I named it XmlFileSystemWriter. The purpose here is to add a method that will write out a directory recursively, to the Xml, in the format you want. Here's what I ended up with:using System; using System.IO; using System.Xml; using System.Text; namespace TestApp { public class XmlFileSystemWriter : XmlTextWriter { public XmlFileSystemWriter(TextWriter w) : base(w) { } public XmlFileSystemWriter(string fileName, Encoding encoding) : base(fileName, encoding) { } public XmlFileSystemWriter(Stream stream, Encoding encoding) : base(stream, encoding) { } public void WriteFileSystemInfo(DirectoryInfo baseDir) { // Create the root element - name can be changed as desired this.WriteStartElement("fileSystemInfo"); // begin recursion this.WriteDirectory(baseDir); // write closing root element this.WriteEndElement(); } private void WriteFile(FileInfo info) { /* write file details in following format * <file> * <name></name> * <path></path> * </file> */ this.WriteStartElement("file"); this.WriteElementString("name",info.Name); this.WriteElementString("path",info.FullName); this.WriteEndElement(); } private void WriteDirectory(DirectoryInfo info) { /* write directory details in following format * <category> * <name></name> * <file/> * <file/> * ...... * </category> */ this.WriteStartElement("category"); this.WriteElementString("name",info.Name); foreach(DirectoryInfo subDir in info.GetDirectories()) WriteDirectory(subDir); foreach(FileInfo file in info.GetFiles()) WriteFile(file); this.WriteEndElement(); } } }

      Usage should be simple enough to work out: // from a console application DirectoryInfo di = new DirectoryInfo("C:\\yourdirectory"); XmlFileSystemWriter w = new XmlFileSystemWriter(Console.Out); // Console.Out is a TextWriter w.Formatting = Formatting.Indent; w.WriteFileSystemInfo(di); I have provided all three constructors for you so you can use a TextWriter, write to a specified file, or to a specified Stream - have a play with

      C 1 Reply Last reply
      0
      • J J4amieC

        hi, This is a particularly easy problem to solve as a small recursive function will do all the work for you. The best way I could think to do this was to derive a class from XmlTextWriter - in my case I named it XmlFileSystemWriter. The purpose here is to add a method that will write out a directory recursively, to the Xml, in the format you want. Here's what I ended up with:using System; using System.IO; using System.Xml; using System.Text; namespace TestApp { public class XmlFileSystemWriter : XmlTextWriter { public XmlFileSystemWriter(TextWriter w) : base(w) { } public XmlFileSystemWriter(string fileName, Encoding encoding) : base(fileName, encoding) { } public XmlFileSystemWriter(Stream stream, Encoding encoding) : base(stream, encoding) { } public void WriteFileSystemInfo(DirectoryInfo baseDir) { // Create the root element - name can be changed as desired this.WriteStartElement("fileSystemInfo"); // begin recursion this.WriteDirectory(baseDir); // write closing root element this.WriteEndElement(); } private void WriteFile(FileInfo info) { /* write file details in following format * <file> * <name></name> * <path></path> * </file> */ this.WriteStartElement("file"); this.WriteElementString("name",info.Name); this.WriteElementString("path",info.FullName); this.WriteEndElement(); } private void WriteDirectory(DirectoryInfo info) { /* write directory details in following format * <category> * <name></name> * <file/> * <file/> * ...... * </category> */ this.WriteStartElement("category"); this.WriteElementString("name",info.Name); foreach(DirectoryInfo subDir in info.GetDirectories()) WriteDirectory(subDir); foreach(FileInfo file in info.GetFiles()) WriteFile(file); this.WriteEndElement(); } } }

        Usage should be simple enough to work out: // from a console application DirectoryInfo di = new DirectoryInfo("C:\\yourdirectory"); XmlFileSystemWriter w = new XmlFileSystemWriter(Console.Out); // Console.Out is a TextWriter w.Formatting = Formatting.Indent; w.WriteFileSystemInfo(di); I have provided all three constructors for you so you can use a TextWriter, write to a specified file, or to a specified Stream - have a play with

        C Offline
        C Offline
        cwayman
        wrote on last edited by
        #3

        Hey Jamie, Your help proved real usefull, I have got it working writing dir structure to xml. I changed my schema though to: Test1 C:\Test1 Test2 C:\Test2 The only problems i seem to be having now is recursing through the XML document to add nodes to tree view so the category attributes are the Parent and child tree nodes and the information is the files within either nodes. So for example from the above XML Directory Name - Sub Category - Test2 - Test1 Any help would also be appreciated on this matter, thanks very much for your effort so far. Kind regards Chris

        C 1 Reply Last reply
        0
        • C cwayman

          Hey Jamie, Your help proved real usefull, I have got it working writing dir structure to xml. I changed my schema though to: Test1 C:\Test1 Test2 C:\Test2 The only problems i seem to be having now is recursing through the XML document to add nodes to tree view so the category attributes are the Parent and child tree nodes and the information is the files within either nodes. So for example from the above XML Directory Name - Sub Category - Test2 - Test1 Any help would also be appreciated on this matter, thanks very much for your effort so far. Kind regards Chris

          C Offline
          C Offline
          cwayman
          wrote on last edited by
          #4

          Solved my problem, thanks for the help chris

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups