ノードを含む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>
<AppXmlLogWritter>
XMLファイルのルートノードを置き換えて、いくつかの新しいノードを追加したいと思います。古いノードを上書きせずに。しかし、私のコードは古いノードを新しいノードに置き換えますその古いノードを新しいノードに保持したい
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();
}
つまり、古いノードと新しいノードの両方が、古いノードを置き換えることなくファイルに保存されます。