C
C#3y ago
yatta

❔ How to write Process name into XML file ?

I try to write my Process name into xml file like this:
foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}
foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}
but when I try to run the code, it skip the process name for first 2 records, and only from 3rd record onwards, it write down enough information of it.
2 Replies
yatta
yattaOP3y ago
this is the full code
if (File.Exists("ProcessKilled.xml"))
{
XmlDocument processedData = new XmlDocument();
processedData.Load("ProcessKilled.xml");

XmlNode root = processedData.SelectSingleNode("/Root");

XmlNode processNode = processedData.CreateElement("Process");
root.AppendChild(processNode);

XmlAttribute nameAttr = processedData.CreateAttribute("Name");
nameAttr.Value = ProcessName;
processNode.Attributes.Append(nameAttr);


foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}

XmlNode killedTimeNode = processedData.CreateElement("KilledTime");
killedTimeNode.AppendChild(processedData.CreateTextNode(DateTime.Now.ToString()));
processNode.AppendChild(killedTimeNode);
processedData.Save("ProcessKilled.xml");
if (File.Exists("ProcessKilled.xml"))
{
XmlDocument processedData = new XmlDocument();
processedData.Load("ProcessKilled.xml");

XmlNode root = processedData.SelectSingleNode("/Root");

XmlNode processNode = processedData.CreateElement("Process");
root.AppendChild(processNode);

XmlAttribute nameAttr = processedData.CreateAttribute("Name");
nameAttr.Value = ProcessName;
processNode.Attributes.Append(nameAttr);


foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}

XmlNode killedTimeNode = processedData.CreateElement("KilledTime");
killedTimeNode.AppendChild(processedData.CreateTextNode(DateTime.Now.ToString()));
processNode.AppendChild(killedTimeNode);
processedData.Save("ProcessKilled.xml");
Accord
Accord3y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server