How to update XML file, if WB_SESSION_ID exists need to update else need to insert.
Inserting part already done need to differentiate insert and update code.
public string GetPriorityRecordingSessionsbeta()
{
var checkoracle = CheckConnection();
DateTime utc1 = this.objDTC.ConvertLocalTimeToUTC(Convert.ToDateTime(this.d_1.ToShortDateString()), ConfigurationManager.AppSettings["TimeZone"].ToString());
DateTime utc2 = this.objDTC.ConvertLocalTimeToUTC(Convert.ToDateTime(this.d_2.ToShortDateString()), ConfigurationManager.AppSettings["TimeZone"].ToString());
ELBASEDAL elbasedal = new ELBASEDAL();
try
{
if (checkoracle)
{
OracleParameter[] parameters = new OracleParameter[3]
{
elbasedal.CreateInParameter("ifromdate", OracleType.VarChar, "5/01/2022 6:30:00 PM"),
elbasedal.CreateInParameter("itodate", OracleType.VarChar, "5/18/2022 6:30:00 PM"),
elbasedal.CreateOutParameter("cursor_s", OracleType.Cursor)
};
DataTable tableStoreProcedure = elbasedal.GetDataTableStoreProcedure("DAILY_REPORTS_PACKAGE.getPriorityRecDetails", parameters);
if (tableStoreProcedure.Rows.Count > 0)
{
try
{
foreach (DataRow row in (InternalDataCollectionBase)tableStoreProcedure.Rows)
{
XMLSessiondetailsLogger("session details logged successfully", row["SESSION_ID"].ToString(), row["WB_SESSION_ID"].ToString(), row["recording_priority"].ToString(), row["Subject_Name"].ToString(), row["Topic_Name"].ToString(), row["Question"].ToString(), row["Student_Name"].ToString(), row["Tutor_Name"].ToString(), row["Franchise_Name"].ToString(), row["Project_Name"].ToString(), row["rec_url"].ToString());
}
InsertDataTosql();
}
catch (Exception ex)
{
this.objerr.WriteXMLErrorLog(ex);
}
}
}
else
{
Console.WriteLine("Failed to start pre-processing. so il sleep task for every 05 min {0}", loop);
//repeat 4 times every 05min
if (loop < 4)
{
// 5 min task sleep
Thread.Sleep(300000);
loop++;
GetPriorityRecordingSessionsbeta();
}
else
{
xmlStatusLogger("fetching Session details from database Failed");
FailuremailSender();
result = "Fail";
}
}
}
catch (Exception ex)
{
this.objerr.WriteXMLErrorLog(ex);
}
return result;
}
public void XMLSessiondetailsLogger(string msg, string msg1, string msg2, string msg3, string msg4, string msg5, string msg6, string msg7, string msg8, string msg9, string msg10, string msg11)
{
string xmlpath;
try
{
_datet = DateTime.Now;
xmlpath = AppDomain.CurrentDomain.BaseDirectory + "XmlEvents";
xmlpath += "\\" + _datet.ToString("dd MMMM yyyy") + "\\SessionDetails";
if (!Directory.Exists(xmlpath))
{
DirectoryInfo di = Directory.CreateDirectory(xmlpath);
}
XmlDocument xmlErrorLogDoc = null;
xmlErrorLogDoc = new XmlDocument();
String xmlErrorLogFileName = xmlpath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".xml";
if (!File.Exists(xmlErrorLogFileName))
{
XmlTextWriter myXmlTextWriter = new XmlTextWriter(xmlErrorLogFileName, Encoding.UTF8);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
myXmlTextWriter.WriteStartElement("Root");
myXmlTextWriter.WriteStartElement("SessionDetails");
myXmlTextWriter.WriteElementString("TIME", DateTime.UtcNow.ToString());
myXmlTextWriter.WriteElementString("Status", msg);
myXmlTextWriter.WriteElementString("SESSION_ID", msg1);
myXmlTextWriter.WriteElementString("WB_SESSION_ID", msg2);
myXmlTextWriter.WriteElementString("recording_priority", msg3);
myXmlTextWriter.WriteElementString("Subject_Name", msg4);
myXmlTextWriter.WriteElementString("Topic_Name", msg5);
myXmlTextWriter.WriteElementString("Question", msg6);
myXmlTextWriter.WriteElementString("Student_Name", msg7);
myXmlTextWriter.WriteElementString("Tutor_Name", msg8);
myXmlTextWriter.WriteElementString("Franchise_Name", msg9);
myXmlTextWriter.WriteElementString("Project_Name", msg10);
myXmlTextWriter.WriteElementString("rec_url", msg11);
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
}
else
{
xmlErrorLogDoc.Load(xmlErrorLogFileName);
XmlNode xmlNodeObj = xmlErrorLogDoc.DocumentElement;
XmlElement childElement1 = xmlErrorLogDoc.CreateElement("SessionDetails");
XmlElement childElement2 = xmlErrorLogDoc.CreateElement("TIME");
XmlElement childElement3 = xmlErrorLogDoc.CreateElement("Status");
XmlElement childElement4 = xmlErrorLogDoc.CreateElement("SESSION_ID");
XmlElement childElement5 = xmlErrorLogDoc.CreateElement("WB_SESSION_ID");
XmlElement childElement6 = xmlErrorLogDoc.CreateElement("recording_priority");
XmlElement childElement7 = xmlErrorLogDoc.CreateElement("Subject_Name");
XmlElement childElement8 = xmlErrorLogDoc.CreateElement("Topic_Name");
XmlElement childElement9 = xmlErrorLogDoc.CreateElement("Question");
XmlElement childElement10 = xmlErrorLogDoc.CreateElement("Student_Name");
XmlElement childElement11 = xmlErrorLogDoc.CreateElement("Tutor_Name");
XmlElement childElement12 = xmlErrorLogDoc.CreateElement("Franchise_Name");
XmlElement childElement13 = xmlErrorLogDoc.CreateElement("Project_Name");
XmlElement childElement14 = xmlErrorLogDoc.CreateElement("rec_url");
childElement2.InnerText = DateTime.UtcNow.ToString();
childElement3.InnerText = msg;
childElement4.InnerText = msg1;
childElement5.InnerText = msg2;
childElement6.InnerText = msg3;
childElement7.InnerText = msg4;
childElement8.InnerText = msg5;
childElement9.InnerText = msg6;
childElement10.InnerText = msg7;
childElement11.InnerText = msg8;
childElement12.InnerText = msg9;
childElement13.InnerText = msg10;
childElement14.InnerText = msg11;
xmlNodeObj.AppendChild(childElement1);
childElement1.AppendChild(childElement2);
childElement1.AppendChild(childElement3);
childElement1.AppendChild(childElement4);
childElement1.AppendChild(childElement5);
childElement1.AppendChild(childElement6);
childElement1.AppendChild(childElement7);
childElement1.AppendChild(childElement8);
childElement1.AppendChild(childElement9);
childElement1.AppendChild(childElement10);
childElement1.AppendChild(childElement11);
childElement1.AppendChild(childElement12);
childElement1.AppendChild(childElement13);
childElement1.AppendChild(childElement14);
xmlErrorLogDoc.Save(xmlErrorLogFileName);
}
}
catch (Exception ex)
{
// objex.WriteXMLErrorLog(ex, "XmlCreater", "XMLMailLogger", "", "");
}
}
Below code what i tried for updating so could you please help me in that https://ibb.co/r001yJH in this link i attached my xml file.
XDocument xxdoc = XDocument.Load(xmlErrorLogFileName); ////Update
var target = (from details in xxdoc.Descendants("Root").Descendants("SessionDetails")
where details.Descendants("WB_SESSION_ID").FirstOrDefault().Value.Trim() == msg2
select new { WB_SESSION_ID = details.Element("WB_SESSION_ID").Value }).ToList();
if (target != null)
{
}