0

Trying to create XML file from xml File from FileStreame Writer class but it gives me an error lile 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.

It creates the <?xml version='1.0' encoding='utf-8' standalone='yes'?> and Root Element Tag Twice my code

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);

thise code is executed more than one time but i just want to prevent to creating the <?xml version='1.0' encoding='utf-8' standalone='yes'?> and root element twise

Steve B
  • 36,818
  • 21
  • 101
  • 174
IMMORTAL
  • 2,707
  • 3
  • 21
  • 37
  • Just a guess, try to flush the stream and set it's position to 0 before the call to xmlDoc.Save – Steve B Jan 15 '13 at 13:35
  • You have asked the same question [here](http://stackoverflow.com/questions/14337786/io-exception-was-unhandled/14337843), after your initial problem was solved. – e_ne Jan 15 '13 at 13:36

1 Answers1

0

You're calling Load (which will leave the stream at the end of the existing file), and then calling Save. Don't do that on the same stream. You could just reposition it, but then you'll end up with problems if the new document is shorter. (It wouldn't be at the moment, but maybe in future.)

I would strongly suggest that you split this into three sections:

  • Create a stream which just reads, and load the existing document
  • Modify the document in memory
  • Create a stream which just writes (truncating the existing file), and save the document
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Doesn't this have the potential to hit performance?(load+parse+save an ever increasing file) You could cache log writes but then, you have a chance that you may loose the events if the app dies. Better use a file format that doesn't require a terminator, you could append write XML to a file, leaving the final close tag off, and add it when you need to parse it. – Mesh Jan 15 '13 at 13:49
  • @Adrian: Sure, that makes sense - but it may not be an issue for the OP, and certainly wasn't the subject of the question. – Jon Skeet Jan 15 '13 at 13:51