Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:15:56+00:00 2026-06-17T09:15:56+00:00

Trying to create XML file from xml File from FileStreame Writer class but it

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:15:56+00:00Added an answer on June 17, 2026 at 9:15 am

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cross-post http://perlmonks.org/index.pl?node_id=979710 I'm trying to create a text file from some XML using Perl
I am trying to create an XML file, but the XML file needs to
I am trying to create an XSD schema file from an existing XML file
I have a problem when trying to create an XML file from registry. On
I am trying to create a dynamically generated XML file from Java. This is
I am trying to create an XML file based on data fields from a
Trying to create a custom component that gets it's layout from an XML file
I'm trying to create a new xml file with parts from other xml files.
I am trying to create hash from xml file Hash.from_xml <<-EOX <user> <id>1</id> <user-name>ryan</user-name>
I am trying to create a new XML file from an exisiting one using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.