Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a big problem. I want to find which xml file is currently executing in no.of xml files and which file execution completed. I want to find the file names. I am using for loop for inserting the xml data into database using c#.

this is code.
C#
var filenames = Directory.GetFiles(@"D:\\xmljobs", "*.xml");
Parallel.ForEach(filenames, filename =>
{
XmlReader reader = XmlReader.Create(filename);
XmlDocument Document = new XmlDocument();
Document.Load(reader);
ReadJobsFromFeed(Document);
});

In the above code I am reading files in ReadJobsFromFeed() method. In that method which file is currently running and completed. I want file names.
C#
ReadJobsFromFeed(XmlDocument Document)
 {
 XmlDocument xmlDocument = Document;
 XmlNode xmlNode = null;
 XmlNode channel = null;
 XmlNode item;

 for (int i = 0; i < xmlDocument.ChildNodes.Count; i++)
 {
 if (xmlDocument.ChildNodes[i].Name == "WEBHARVY_DATA")
 {
 channel = xmlDocument.ChildNodes[i];


 }

 }

 int num = 0;
 for (int i = 0; i < channel.ChildNodes.Count; i++)
 {
 if (channel.ChildNodes[i].Name == "item")
 {
 num++;

 }

 }
 var datarray = new string[num, 10];
 num = 0;


 for (int i = 0; i < channel.ChildNodes.Count; i++)
 {
 // cc = cc + 1;
 // Console.WriteLine("cc count"+cc);
 try
 {
 if (channel.ChildNodes[i].Name == "item")
 {

 item = channel.ChildNodes[i];
 if (item["Title"].InnerText != "More Jobs Available on Career Section...")
 {
 datarray[num, 0] = item["Title"].InnerText;
 datarray[num, 1] = item["Link"].InnerText;
 datarray[num, 2] = item["Description"] == null ? string.Empty : item["Description"].InnerText.Trim();
 }
 }
 }
 }
 }

The above code is implemented in readjobsfromfeed() method.

In last for loop my inserting code there. At the end of the program I want file name which file currently executing and completed filenames.

my problem is After insertion completed from xml file, I want to move the file to other location.

Please anyone help me.

Thank you.
Posted
Updated 6-May-14 6:19am
v2
Comments
Karthik Harve 6-May-14 0:45am    
I didn't understood your question. I mean, if you are using FOR loop, definitely you know which file you are accessing. So what is that causing confusion ? are you using threading ??
Per Söderlund 6-May-14 0:58am    
No one can answer this unless you give us some code that is not working or needs to be improved.
NagaRaju Pesarlanka 6-May-14 1:12am    
I have a big problem. I want to find which xml file is currently executing in no.of xml files and which file execution completed. I want to find the file names. I am using for loop for inserting the xml data into database using c#.

this is code.

var filenames = Directory.GetFiles(@"D:\\xmljobs", "*.xml");
Parallel.ForEach(filenames, filename =>
{
XmlReader reader = XmlReader.Create(filename);
XmlDocument Document = new XmlDocument();
Document.Load(reader);
ReadJobsFromFeed(Document);
});

In the above code I am reading files in ReadJobsFromFeed() method. In that method which file is currently running and completed. I want file names.


ReadJobsFromFeed(XmlDocument Document)
{
XmlDocument xmlDocument = Document;
XmlNode xmlNode = null;
XmlNode channel = null;
XmlNode item;

for (int i = 0; i < xmlDocument.ChildNodes.Count; i++)
{
if (xmlDocument.ChildNodes[i].Name == "WEBHARVY_DATA")
{
channel = xmlDocument.ChildNodes[i];


}

}

int num = 0;
for (int i = 0; i < channel.ChildNodes.Count; i++)
{
if (channel.ChildNodes[i].Name == "item")
{
num++;

}

}
var datarray = new string[num, 10];
num = 0;


for (int i = 0; i < channel.ChildNodes.Count; i++)
{
// cc = cc + 1;
// Console.WriteLine("cc count"+cc);
try
{
if (channel.ChildNodes[i].Name == "item")
{

item = channel.ChildNodes[i];
if (item["Title"].InnerText != "More Jobs Available on Career Section...")
{
datarray[num, 0] = item["Title"].InnerText;
datarray[num, 1] = item["Link"].InnerText;
datarray[num, 2] = item["Description"] == null ? string.Empty : item["Description"].InnerText.Trim();
}
}
}
}
}

The above code is implemented in readjobsfromfeed() method.
Please anyone help me.

Thank you.
gggustafson 6-May-14 12:13pm    
Everything you provided here and in the next two replies belong in your original question. I will attempt to repair your question.
NagaRaju Pesarlanka 6-May-14 1:14am    
In last for loop my inserting code there. At the end of the program I want file name which file currently executing and completed filenames.

1 solution

Because you are using a Parallel.ForEach() the XML file "currently" being processed is sort of meaningless.
Depending on the parallelism the Parallel.ForEach accomplishes several XML files could be in different states of processing at the same time.
For starters, you load each file and then never refer to that filename again.
Should the file be "moved to another location" after the file load is completed? Or not until ReadJobsFromFeed() has completed?
If the former, then put the file moving in the Parallel.ForEach() right after the Load.
If the latter, then put it after ReadJobsFromFeed returns.

It might be simpler if ReadJobsFromFeed took the filename as the argument and loaded the XmlDocument itself. Then it could move the XML file whenever it was appropriate:
C#
Parallel.ForEach(filenames, ReadJobsFromFeed);

and
C#
ReadJobsFromFeed(string filename)
{
   XmlDocument xmlDocument = new XmlDocument();
   using (XmlReader reader = XmlReader.Create(filename))
   {
     xmlDocument.Load(reader);
   }
   // move file here?
   // all processing of the XmlDocument
   // OR move file here.
}

Other issues/questions with your code.
1. The XmlReader is not disposed correctly (see above, or):
C#
var filenames = Directory.GetFiles(@"D:\\xmljobs", "*.xml");
Parallel.ForEach(filenames, filename =>
{
  XmlDocument Document = new XmlDocument();
  using (XmlReader reader = XmlReader.Create(filename))
  {
    Document.Load(reader);
  }
  ReadJobsFromFeed(Document);
});

2. channel is being set to the LAST node with Name of WEBHARVY_DATA. Is that really what you want?

3. Several of the steps in ReadJobsFromFeed would be much simpler implemented using Linq.
E.g., instead of:
C#
int num = 0;
for (int i = 0; i < channel.ChildNodes.Count; i++)
{
if (channel.ChildNodes[i].Name == "item")
{
num++;
}
}

use:
C#
int num = channel.ChildNodes.Cast<XmlNode>().Count(cn => cn.Name == "item");

and others.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900