Now i am updating large multiple files in foreach loop which is taking time. so curious to know can i use Parallel.ForEach to update multiple large xml files simultaneously but do not want to use Lock()
I am new in Parallel.ForEach so scared for race condition and improper updation of xml file. data should not overlap in files. here is one sample code. so please guys see my code and tell me does my code work fine in production ?
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);
});
please guide me with best approach which i can use to update multiple large xml files with in short times. thanks
Since each running case of Parallel.For
is updating a separate file, you do not risk a race condition, and you don't need locks. But don't expect miracles - it is quite likely that the hard-drive / SSD will be your performance bottleneck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.