TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sateesh Kumar
1.6k
101
426
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
List<
string
> xmlFiles =
new
List<
string
>(Directory.GetFiles(sourceFolder,
"*.xml"
, SearchOption.AllDirectories));
LoadReadWriteSettings();
// loading xml reder ,writer settings and xsl load settings.
foreach
(
string
file
in
xmlFiles)
{
if
(IsValidFile(file))
{
Task task = Task.Run(() => ReadXml(file));
tasks.Add(task);
if
(tasks.Count == 200)
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
LogData(content, outputFlie);
// after reading 200 files, writing into text file.
content =
new
StringBuilder();
}
}
}
internal
async
void
ReadXml(
string
xmlFile)
{
try
{
StringBuilder lines =
new
StringBuilder();
XmlReader reader = System.Xml.XmlReader.Create(xmlFile, readerSettings);
XmlWriter results = XmlWriter.Create((TextWriter)
new
StringWriter(lines), writerSettings);
transform.Transform(reader,
null
, results);
if
(String.IsNullOrWhiteSpace(lines.ToString()))
emptyFiles.Add(Path.GetFileName(xmlFile));
else
content.Append(lines.ToString());
// content here StringBuilder
}
catch
(Exception ex)
{
errorFiles.Add(Path.GetFileName(xmlFile));
LogException(ex);
}
}
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
Reply
Answers (
1
)
Conversions from c# to odbc sql server
Interop Excel : Copy sheet and put it in another workbook