C#使用Parallel.ForEach更新多个xml文件
现在我正在更新foreach循环中的多个大型文件,这需要时间。非常想知道我是否可以使用Parallel.ForEach同时更新多个大型xml文件,但不想使用Lock() 我是Parallel.ForEach的新手,非常害怕竞争条件和xml文件的不正确更新。文件中的数据不应重叠。下面是一个示例代码。所以请大家看看我的代码,告诉我我的代码在生产中工作得好吗C#使用Parallel.ForEach更新多个xml文件,c#,task-parallel-library,C#,Task Parallel Library,现在我正在更新foreach循环中的多个大型文件,这需要时间。非常想知道我是否可以使用Parallel.ForEach同时更新多个大型xml文件,但不想使用Lock() 我是Parallel.ForEach的新手,非常害怕竞争条件和xml文件的不正确更新。文件中的数据不应重叠。下面是一个示例代码。所以请大家看看我的代码,告诉我我的代码在生产中工作得好吗 List<string> listoffiles = new List<string>(); listo
List<string> listoffiles = new List<string>();
listoffiles.Add(@"d:\test1.xml");
listoffiles.Add(@"d:\test2.xml");
listoffiles.Add(@"d:\test3.xml");
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 };
Parallel.ForEach(listoffiles, options, (filepath) =>
{
XDocument xmlDoc = XDocument.Load(filepath);
//query 10QK xml data based on section & lineitem
var items = (from item in xmlDoc.Descendants("TickerBrokerStandardDateLineitemValue")
where item.Element("TabName").Value.Trim() == data.Section
&& item.Element("StandardLineItem").Value.Trim() == data.LineItem
select item).ToList();
foreach (var item in items)
{
//element will be inserted or updated in xml when match found
if (item.Element("SectionID") == null)
{
//if SectionID element does not exist then it will be added in xml having ID
item.Add(new XElement("SectionID", data.SectionID));
}
else
{
//if SectionID element exist then it will be updated with db value
item.Element("SectionID").SetValue(data.SectionID);
}
//element will be inserted or updated in xml when match found
if (item.Element("LineItemID") == null)
{
//if LineItemID element does not exist then it will be added in xml having ID
item.Add(new XElement("LineItemID", data.LineItemID));
}
else
{
//if LineItemID element exist then it will be updated with db value
item.Element("LineItemID").SetValue(data.LineItemID);
}
if (data.Approved == 1)
{
//if approved then xfundcode will be updated
if (item.Element("XFundCode") != null)
{
//if XFundCode element exist then it will be updated with db value
item.Element("XFundCode").SetValue(data.ApprovedXFundCode);
}
}
else if (data.Approved == 0)
{
//if unapproved then xfundcode will be empty
if (item.Element("XFundCode") != null)
{
//if XFundCode element exist then it will be updated with db value
item.Element("XFundCode").SetValue(string.Empty);
}
}
}
xmlDoc.Save(filepath);
});
List listoffiles=new List();
添加(@“d:\test1.xml”);
添加(@“d:\test2.xml”);
添加(@“d:\test3.xml”);
var options=new ParallelOptions{maxdegreeofpparallelism=Environment.ProcessorCount*10};
Parallel.ForEach(文件列表,选项,(文件路径)=>
{
XDocument xmlDoc=XDocument.Load(文件路径);
//基于section&lineitem查询10QK xml数据
var items=(来自xmlDoc.subjects(“TickerBrokerStandardDateLineitemValue”)中的项)
其中item.Element(“TabName”).Value.Trim()==data.Section
&&item.Element(“StandardLineItem”).Value.Trim()==data.LineItem
选择项);
foreach(项目中的var项目)
{
//当找到匹配项时,元素将以xml形式插入或更新
if(item.Element(“SectionID”)==null)
{
//如果SectionID元素不存在,那么它将添加到具有ID的xml中
添加(新的元素(“SectionID”,数据.SectionID));
}
其他的
{
//如果SectionID元素存在,则将使用db值更新它
item.Element(“SectionID”).SetValue(数据.SectionID);
}
//当找到匹配项时,元素将以xml形式插入或更新
if(item.Element(“LineItemID”)==null)
{
//如果LineItemID元素不存在,那么它将添加到具有ID的xml中
添加(新的XElement(“LineItemID”,data.LineItemID));
}
其他的
{
//如果LineItemID元素存在,则将使用db值更新它
item.Element(“LineItemID”).SetValue(data.LineItemID);
}
如果(data.Approved==1)
{
//如果获得批准,则将更新xfundcode
if(item.Element(“XFundCode”)!=null)
{
//如果XFundCode元素存在,则将使用db值对其进行更新
item.Element(“XFundCode”).SetValue(数据批准的XFundCode);
}
}
否则如果(data.Approved==0)
{
//如果未批准,则xfundcode将为空
if(item.Element(“XFundCode”)!=null)
{
//如果XFundCode元素存在,则将使用db值对其进行更新
item.Element(“XFundCode”).SetValue(string.Empty);
}
}
}
xmlDoc.Save(文件路径);
});
请指导我如何在短时间内更新多个大型xml文件。感谢,因为
的每个运行案例都是并行的。因为正在更新一个单独的文件,所以您不需要冒竞争条件的风险,也不需要锁。但不要期待奇迹-硬盘驱动器/SSD很可能会成为您的性能瓶颈。什么是项
和项
?作为一个一般提示,为了更好地了解并行编程的改进程度,通读阿姆达尔定律——它证明了为什么有时即使在进行并行编程时,我们也无法获得预期的性能改进。