簡體   English   中英

如何僅創建一次根標記

[英]How to create Root Tag only once

試圖從FileStreame Writer類的xml File創建XML文件,但它給我一個錯誤提示Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.

它創建<?xml version='1.0' encoding='utf-8' standalone='yes'?>並且創建根元素標簽兩次

using (FileStream fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                    xmlDoc.Load(fileStream);
                    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");

                    xmlLogID.InnerText = _logIDPrefix + currentDateTime + 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);

                    xmlDoc.DocumentElement.AppendChild(newelement);
                    xmlDoc.Save(fileStream);

此代碼已執行多次,但我只想防止創建<?xml version='1.0' encoding='utf-8' standalone='yes'?> 1.0'encoding <?xml version='1.0' encoding='utf-8' standalone='yes'?>和根元素twise

你調用Load (這將在現有文件的末尾離開流),然后調用Save 不要在同一流上執行此操作。 可以重新放置它,但是如果新文檔較短,則會遇到問題。 (暫時不會,但將來可能會。)

我強烈建議您將其分為三個部分:

  • 創建一個僅讀取並加載現有文檔的流
  • 修改內存中的文檔
  • 創建一個僅寫入(截斷現有文件)的流,然后保存文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM