0

有一个带有节点的 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();
            }

即旧节点和新节点都保存到文件而不替换旧节点。

4

1 回答 1

0

我已经解决了这个问题,现在变得比 XmlDocument 代码的加载方法快得多,我只是替换了正确工作的 try 块,并保持原样的代码

            string rootEndTag = "</AppXmlLogWritter>";
            int rootEndTagLength = Encoding.UTF8.GetBytes(rootEndTag).Length;
            try
            {
                objMutex.WaitOne();
                if (!File.Exists(_logFilePath))
                {
                    File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?><AppXmlLogWritter></AppXmlLogWritter>");
                }

                var fileStream = File.Open(_logFilePath, FileMode.Open);
                fileStream.Position = fileStream.Length - rootEndTagLength;

                var objStreamWriter = new StreamWriter(fileStream);
                objStreamWriter.WriteLine(newelement.OuterXml);
                objStreamWriter.Write(rootEndTag);
                objStreamWriter.Close();
                fileStream.Close();
            }
于 2013-02-08T12:45:17.493 回答