Writing DirectoryInfo() to XML File
-
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
-
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
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 -
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 withHey 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
-
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