If got a method that returns a treenode based upon a Directory
private static TreeNode GetDirectoryNodes(string path)
{
var dir = new DirectoryInfo(path);
var node = new TreeNode(dir.Name);
foreach (var directory in dir.GetDirectories())
{
node.Nodes.Add(GetDirectoryNodes(path + "\\" + directory.ToString()));
}
return node;
}
However I need to create an XML DOM of the directory stucture, However I am new to XML DOM and can’t figure out how to do this. Problems that I see are: how to get \ into the XML; and how to get Subdirectories This is what I’ve got so far
private static XmlDocument GetDirTreeData(string path)
{
var dir = new DirectoryInfo(path);
XmlDocument XMLDOM = new XmlDocument();
XmlElement xl = XMLDOM.CreateElement(path);
foreach (var directory in dir.GetDirectories())
{
xl.InnerXml = directory.ToString();
}
return XMLDOM;
}
Take a look at LINQ to XML. It is easier to accomplish your task with LINQ.
Here is a code that works, but does not handle Access denied and similar issues
xdoc variable is your xml document. DirToXml is recursive method that finds all subdirectories and creates elements for each of them.
The results looks like this: