❔ How to write Process name into XML file ?

Yyatta11/10/2022
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);
                }

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.
Yyatta11/10/2022
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");
AAccord11/22/2022
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.