在c#和#x2B中插入不带xmldomment.Load()的xml节点;XML

在c#和#x2B中插入不带xmldomment.Load()的xml节点;XML,c#,xml,C#,Xml,有一个包含节点的log.xml文件 <AppXmlLogWritter> <LogData> <LogID>5678201302071614556349583069537201151138</LogID> <LogDateTime>20130207161455</LogDateTime> <LogType>Warning</LogType> <LogFlag&

有一个包含节点的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>

5678201302071614556349583069537201151138
20130207161455
警告
巴布罗格弗拉格
巴氏涂片
BaburaoLogModule
巴氏舌音
BaburaoLogText
5678201302071614556349583069537201151138
20130207161455
警告
巴布罗格弗拉格
巴氏涂片
BaburaoLogModule
巴氏舌音
BaburaoLogText
我想通过替换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();
            }
Mutex objMutex=new Mutex(false,@“Global\MySharedLog”);
XmlDocument xmlDoc=新的XmlDocument();
字符串currentDateTime=DateTime.Now.ToString(“yyyyMMddHHmmss”);
xmlement newelement=xmlDoc.CreateElement(“日志数据”);
xmlement 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=\u logIDPrefix+currentDateTime+DateTime.UtcNow.Ticks+randomNumber;
xmlLogDateTime.InnerText=currentDateTime;
xmlLogType.InnerText=((日志类型)Convert.ToInt32(日志类型)).ToString();
xmlLogFlag.InnerText=logFlag;
xmlLogApplication.InnerText=\u logApplication;
xmlLogModule.InnerText=logModule;
xmlLogLocation.InnerText=logLocation;
xmlLogText.InnerText=logText;
xmlLogStackTrace.InnerText=logStackTrace;
newelement.AppendChild(xmlLogID);
AppendChild(xmlLogDateTime);
AppendChild(xmlLogType);
newelement.AppendChild(xmlLogFlag);
AppendChild(xmlLogApplication);
AppendChild(xmlLogModule);
AppendChild(xmlLogLocation);
newelement.AppendChild(xmlLogText);
尝试
{
objMutex.WaitOne();
如果(!File.Exists(_logFilePath))
{
writealText(_logFilePath,“\n\n”);
}
string fileContains=string.Empty;
foreach(File.ReadLines(_logFilePath))中的字符串行
{
如果(第行包含(“”))
{
fileContains=line.Replace(“,”+newelement.OuterXml.ToString());
打破
}
}
//xmlDoc.PreserveWhitespace=true;
LoadXml(fileContains);
////xmlDoc.Load(_logFilePath);
xmlement ele=xmlDoc.DocumentElement;
xmlDoc.DocumentElement.AppendChild(新元素);
xmlDoc.Save(_logFilePath);
}
最后
{
objMutex.ReleaseMutex();
}

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

我已经解决了这个问题,现在比加载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();
            }
字符串rootEndTag=”“;
int rootEndTagLength=Encoding.UTF8.GetBytes(rootEndTag).Length;
尝试
{
objMutex.WaitOne();
如果(!File.Exists(_logFilePath))
{
writealText(_logFilePath,“”);
}
var fileStream=File.Open(_logFilePath,FileMode.Open);
fileStream.Position=fileStream.Length-rootEndTagLength;
var objStreamWriter=newstreamwriter(fileStream);
objStreamWriter.WriteLine(newelement.OuterXml);
objStreamWriter.Write(rootEndTag);
objStreamWriter.Close();
fileStream.Close();
}

您能给出生成的xml的示例吗,您的解释对我来说没有意义。