如何查找当前正在执行的文件以及在大量xml文件中执行的文件。 [英] how to find which file executing currently and which file executing completed in bulk of xml files.

查看:105
本文介绍了如何查找当前正在执行的文件以及在大量xml文件中执行的文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大问题。我想在no.of xml文件中找到当前正在执行的xml文件以及完成了哪个文件执行。我想找到文件名。我使用for循环将xml数据插入数据库使用c#。



这是代码。

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



在上面的代码中,我正在读取ReadJobsFromFeed()方法中的文件。在该方法中哪个文件当前正在运行并完成。我想要文件名。

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

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);
尝试
{
if (channel.ChildNodes [i] .Name == item
{

item = channel.ChildNodes [i];
if (item [ Title].InnerText!= 更多职位可用于职业部分......
{
datarray [num, 0 ] = item [ 标题]的innerText。
datarray [num, 1 ] = item [ 链接]的innerText。
datarray [num, 2 ] = item [ 说明] == null string .Empty:item [ Description] InnerText.Trim();
}
}
}
}
}



以上代码在readjobsfromfeed()方法中实现。



在最后一个循环我的插入代码那里。在程序结束时,我想要文件名当前正在执行的文件和完成的文件名。



我的问题是从xml文件插入完成后,我想移动文件到其他地方。



请有人帮帮我。



谢谢。

解决方案

因为您使用的是 Parallel.ForEach(),所以正在处理的当前XML文件有点无意义。

根据并行性, Parallel.ForEach 完成多个XML文件可能同时处于不同的处理状态。

对于初学者,你加载每个文件然后再也不再引用那个文件名。

文件加载完成后文件是否应该移动到另一个位置?或者直到ReadJobsFromFeed()完成?

如果是前者,那么在加载后立即将文件移入 Parallel.ForEach()

如果是后者,那么将它放在 ReadJobsFromFeed 之后返回。



It如果 ReadJobsFromFeed 将文件名作为参数并加载XmlDocument本身,可能会更简单。然后它可以在适当的时候移动XML文件:

 Parallel.ForEach(filenames,ReadJobsFromFeed); 





 ReadJobsFromFeed( string  filename)
{
XmlDocument xmlDocument = new XmlDocument();
使用(XmlReader reader = XmlReader.Create(filename))
{
xmlDocument.Load(reader);
}
// 在此处移动文件?
// XmlDocument的所有处理
// 或在此处移动文件。
}



您的代码的其他问题/问题。

1. XmlReader配置不正确(见上文或):

  var  filenames = Directory.GetFiles( @  D:\\xmljobs  * .xml); 
Parallel.ForEach(文件名,文件名= >
{
XmlDocument Document = new XmlDocument();
使用(XmlReader reader = XmlReader.Create(filename))
{
Document。加载(读者);
}
ReadJobsFromFeed(Document);
});



2. channel 正被设置为名称为WEBHARVY_DATA的LAST节点。这真的是你想要的吗?



3. ReadJobsFromFeed 中的几个步骤将更加简单实现Linq。

例如,而不是:

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



使用:

  int  num = channel.ChildNodes.Cast< XmlNode>()。Count(cn = >  cn.Name ==   item); 



和其他人。


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.

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.

解决方案

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:

Parallel.ForEach(filenames, ReadJobsFromFeed);


and

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):

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:

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


use:

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


and others.


这篇关于如何查找当前正在执行的文件以及在大量xml文件中执行的文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆