How to update multiple xml files simultaneously by thread

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
Supposed in a list i have stored many xml files path. I need to update each xml files in separate thread. So how could i develop a multi threading program which will update each xml file in separate thread. So when i will run progrthen all xml files will be updating parallel. Also race condition should not occur.

I will use xdocument class to load xml file and in foreach i will add or update xml element. A sample code attached hete which show how i am updating xml file. So i just need to know how could i update multiple xml files at parallel. Please help me with sample code. Thanks

{
XDocument xmlDoc = XDocument.Load(path);

//iterate in return data from db
foreach (var data in returndata)
{
//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(path);

Continue reading...
 
Back
Top