Sateesh Kumar

Sateesh Kumar

  • NA
  • 100
  • 385

Read directory of files and write data into single file.

Jun 8 2020 10:00 AM
Hi Team,
 
Goal:
 
Read 6,00,000 xml files and write into text file.
 
here is my code
  1. List<string> xmlFiles = new List<string>(Directory.GetFiles(sourceFolder, "*.xml", SearchOption.AllDirectories));  
  2. LoadReadWriteSettings(); // loading xml reder ,writer settings and xsl load settings.  
  3. foreach (string file in xmlFiles)  
  4. {  
  5. if (IsValidFile(file))  
  6. {  
  7. Task task = Task.Run(() => ReadXml(file));  
  8. tasks.Add(task);  
  9. if (tasks.Count == 200)  
  10. {  
  11. Task.WaitAll(tasks.ToArray());  
  12. tasks.Clear();  
  13. LogData(content, outputFlie);// after reading 200 files, writing into text file.  
  14. content = new StringBuilder();  
  15. }  
  16. }  
  17. }  
  18. internal async void ReadXml(string xmlFile)  
  19. {  
  20. try  
  21. {  
  22. StringBuilder lines = new StringBuilder();  
  23. XmlReader reader = System.Xml.XmlReader.Create(xmlFile, readerSettings);  
  24. XmlWriter results = XmlWriter.Create((TextWriter)new StringWriter(lines), writerSettings);  
  25. transform.Transform(reader, null, results);  
  26. if (String.IsNullOrWhiteSpace(lines.ToString()))  
  27. emptyFiles.Add(Path.GetFileName(xmlFile));  
  28. else  
  29. content.Append(lines.ToString()); // content here StringBuilder  
  30. }  
  31. catch (Exception ex)  
  32. {  
  33. errorFiles.Add(Path.GetFileName(xmlFile));  
  34. LogException(ex);  
  35. }  
  36. }  
its working fine. but taking too much time. almost 3:00 hours.
 
is there any other way reduce the time.
 
i read about semaphore, am failing to how to use? getting error at semaphore realse();
 
how many max instance can i give to seamphore? (above 16 also can i give. i.e Semaphore(0, 16))
 
Thanks for the advance.
J. Sateesh kumar

Answers (1)