有一个带有节点的 log.xml 文件
<AppXmlLogWritter>
<LogData>
<LogID>5678201302071614556349583069537201151138</LogID>
<LogDateTime>20130207161455</LogDateTime>
<LogType>Warning</LogType>
<LogFlag>BaburaoLogFlag</LogFlag>
<LogApplication>BaburaoLogApplication</LogApplication>
<LogModule>BaburaoLogModule</LogModule>
<LogLocation>BaburaoLogLocation</LogLocation>
<LogText>BaburaoLogText</LogText>
</LogData>
<LogData>
<LogID>5678201302071614556349583069537201151138</LogID>
<LogDateTime>20130207161455</LogDateTime>
<LogType>Warning</LogType>
<LogFlag>BaburaoLogFlag</LogFlag>
<LogApplication>BaburaoLogApplication</LogApplication>
<LogModule>BaburaoLogModule</LogModule>
<LogLocation>BaburaoLogLocation</LogLocation>
<LogText>BaburaoLogText</LogText>
</LogData>
</AppXmlLogWritter>
我想通过替换 XML 文件中的根节点来添加一些新节点<AppXmlLogWritter>
。不覆盖旧节点。但是我的代码用我的新节点替换旧节点我想用新节点保留那个旧节点
Mutex objMutex = new Mutex(false, @"Global\MySharedLog");
XmlDocument xmlDoc = new XmlDocument();
string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
XmlElement newelement = xmlDoc.CreateElement("LogData");
XmlElement xmlLogID = xmlDoc.CreateElement("LogID");
XmlElement xmlLogDateTime = xmlDoc.CreateElement("LogDateTime");
XmlElement xmlLogType = xmlDoc.CreateElement("LogType");
XmlElement xmlLogFlag = xmlDoc.CreateElement("LogFlag");
XmlElement xmlLogApplication = xmlDoc.CreateElement("LogApplication");
XmlElement xmlLogModule = xmlDoc.CreateElement("LogModule");
XmlElement xmlLogLocation = xmlDoc.CreateElement("LogLocation");
XmlElement xmlLogText = xmlDoc.CreateElement("LogText");
XmlElement xmlLogStackTrace = xmlDoc.CreateElement("LogStackTrace");
int randomNumber = random.Next(9999);
xmlLogID.InnerText = _logIDPrefix + currentDateTime + DateTime.UtcNow.Ticks + randomNumber;
xmlLogDateTime.InnerText = currentDateTime;
xmlLogType.InnerText = ((LogTypes)Convert.ToInt32(logType)).ToString();
xmlLogFlag.InnerText = logFlag;
xmlLogApplication.InnerText = _logApplication;
xmlLogModule.InnerText = logModule;
xmlLogLocation.InnerText = logLocation;
xmlLogText.InnerText = logText;
xmlLogStackTrace.InnerText = logStackTrace;
newelement.AppendChild(xmlLogID);
newelement.AppendChild(xmlLogDateTime);
newelement.AppendChild(xmlLogType);
newelement.AppendChild(xmlLogFlag);
newelement.AppendChild(xmlLogApplication);
newelement.AppendChild(xmlLogModule);
newelement.AppendChild(xmlLogLocation);
newelement.AppendChild(xmlLogText);
try
{
objMutex.WaitOne();
if (!File.Exists(_logFilePath))
{
File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-16' standalone='yes'?>\n<AppXmlLogWritter>\n</AppXmlLogWritter>");
}
string fileContains = string.Empty;
foreach (string line in File.ReadLines(_logFilePath))
{
if (line.Contains("<AppXmlLogWritter>"))
{
fileContains = line.Replace("<AppXmlLogWritter>", "<AppXmlLogWritter>" + newelement.OuterXml.ToString());
break;
}
}
//xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(fileContains);
// //xmlDoc.Load(_logFilePath);
XmlElement ele = xmlDoc.DocumentElement;
xmlDoc.DocumentElement.AppendChild(newelement);
xmlDoc.Save(_logFilePath);
}
finally
{
objMutex.ReleaseMutex();
}
即旧节点和新节点都保存到文件而不替换旧节点。