private static XElement RecursiveDirectoryTraversal(Folder folder) { var directoryInfo = new DirectoryInfo(folder.Name); var dirElement = new XElement(isRoot ? "root-dir" : "dir", new XAttribute("name", folder.Name)); isRoot = false; foreach (var file in directoryInfo.GetFiles()) { folder.Files.Add(new File(file.FullName)); var fileElement = new XElement("file", new XAttribute("name", file.Name)); dirElement.Add(fileElement); } foreach (var directory in directoryInfo.GetDirectories()) { var childFolder = new Folder(directory.FullName); folder.ChildFolders.Add(childFolder); dirElement.Add(RecursiveDirectoryTraversal(childFolder)); } return dirElement; }
public static void Main() { var folder = new Folder(folderPath); xDocument.Add(RecursiveDirectoryTraversal(folder)); xDocument.Save("../../XElementDirectory.xml"); }