读取和改写xml有XmlDocument类进行处理,但由于xml文件数量过多,而且操作上可以需要一定的时间,所以考虑用DataSet的ReadXml和WriteXml方法进行处理。工作上要求5分钟执行一次该程序,于是,这里应该用上Thread类。
用线程控制程序的执行 摘自:http://www.lemongtree.com/Detail,306.html
public void Main()
{
//设定永远为真的一个循环
while (true)
{
if (chkPath(_SourcePath) && (chkPath(_GoalPath)))
{
DirectoryInfo di = new DirectoryInfo(_SourcePath);
foreach (FileInfo fi in di.GetFiles())
{
string xmlFileName = fi.Name;
string FullName = fi.FullName;
DataSet dst = new DataSet();
dst.ReadXml(FullName);
if (dst.Tables.Contains("channel") == false)
{
Console.WriteLine("no table " + FullName);
continue;
}
foreach (DataRow r in dst.Tables["channel"].Rows)
{
string[] arrCatalog = r["Catalog"].ToString().Split(' ');
r["catalog"] = arrCatalog[0];
}
dst.WriteXml(_GoalPath + xmlFileName);
dst.Dispose();
dst = null;
}
}
else
{
System.Console.WriteLine("来源目录或目标目录配置错误!请检查配置文件");
string s = Console.ReadLine();
}
//设定5分钟执行一次
Thread.Sleep(_SleepTime * 60 * 1000);
}
}
然后在
[STAThread]
static void Main(string[] args)
static void Main(string[] args)
中调用该方法即可
