Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8.  
  9. namespace lab10_2
  10. {
  11. class Program
  12. {
  13. private const string folderLocation = @"D:\1307B";
  14. static void Main(string[] args)
  15. {
  16. DirectoryInfo dir = new DirectoryInfo(folderLocation);
  17.  
  18. // makes everything wrapped in an XElement called serverfiles.
  19. // Also a declaration as specified (sorry about the standalone status:
  20. // it's required in the XDeclaration constructor)
  21. var doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
  22. CREATEXML(dir));
  23.  
  24. Console.WriteLine(doc.ToString());
  25.  
  26. Console.Read();
  27.  
  28. }
  29.  
  30. private static XElement CREATEXML(DirectoryInfo dir)
  31. {
  32. //get directories
  33. var xmlInfo = new XElement("serverfiles", new XAttribute("name", dir.Name));
  34.  
  35. //get all the files first
  36. foreach (var file in dir.GetFiles())
  37. {
  38. xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
  39. }
  40.  
  41. //get subdirectories
  42. foreach (var subDir in dir.GetDirectories())
  43. {
  44. xmlInfo.Add(CREATEXML(subDir));
  45. }
  46.  
  47. return xmlInfo;
  48.  
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement