How to insert update data when working with XDocument

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

Sudip_inn

Guest
see my code where i am querying xml file by XDocument. if data found then updating like this way item.Element("IgnoreData").SetValue(ignorecelldata);

so i need to know if no data found then how could i insert data for IgnoreData element. please help me with some code. thanks

XDocument xmlDoc = XDocument.Load(filePath);

var items = (from item in xmlDoc.Descendants("clsCommentPopup")
where item.Element("Section").Value.Trim().ToUpper() == section.Trim().ToUpper()
&& item.Element("LineItem").Value.Trim().ToUpper() == li.Trim().ToUpper()
&& item.Element("BrokerFor").Value.Trim().ToUpper() == broker.Trim().ToUpper()
&& item.Element("PeriodCollection").Value.Trim().ToUpper() == Period.Trim().ToUpper()
select item).ToList();

bool IsFieldUpdated = false;

foreach (var item in items)
{
if (ignorecelldata != "")
{
if (_IsIgnored)
item.Element("IgnoreData").SetValue(ignorecelldata);
else
item.Element("IgnoreData").SetValue(string.Empty);

IsFieldUpdated = true;
}
}

if (IsFieldUpdated)
xmlDoc.Save(strCommentPath);

Continue reading...
 
Back
Top