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 8380771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:32:30+00:00 2026-06-09T16:32:30+00:00

I am getting Unexpected XML declaration. The XML declaration must be the first node

  • 0

I am getting “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” error while trying to load xml. Both my C# code and contents of XML file are given below. XML definition exists in Line 6 of the xml file and hence the error.

I can not control what’s there in the xml file so how can I edit/rewrite it using C# such that xml declaration comes first and then the comments to load it without any error!

//xmlFilepath is the path/name of the xml file passed to this function
static function(string xmlFilepath)
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
readerSettings.IgnoreWhitespace = true;
XmlReader reader = XmlReader.Create(XmlFilePath, readerSettings);
XmlDocument xml = new XmlDocument();
xml.Load(reader);
}

XmlDoc.xml

<!-- Customer ID: 1 -->
<!-- Import file: XmlDoc.xml -->
<!-- Start time: 8/14/12 3:15 AM -->
<!-- End time: 8/14/12 3:18 AM -->

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
-----
  • 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-09T16:32:31+00:00Added an answer on June 9, 2026 at 4:32 pm

    As the error states, the first five characters of an XML document should be <?xml. No ifs, ands or buts. The comments you have above the opening XML tag are illegal; they must go inside the XML tag (because the comment structure is itself defined by the XML standard and so is meaningless outside the main XML tags).

    EDIT: Something like this should be able to rearrange the rows, given the file format from the OP:

    var lines = new List<string>();
    
    using (var fileStream = File.Open(xmlFilePath, FileMode.Open, FileAccess.Read))
       using(var reader = new TextReader(fileStream))
       {
          string line;
          while((line = reader.ReadLine()) != null)
             lines.Add(line);
       }   
    
    var i = lines.FindIndex(s=>s.StartsWith("<?xml"));
    var xmlLine = lines[i];
    lines.RemoveAt(i);
    lines.Insert(0,xmlLine);
    
    using (var fileStream = File.Open(xmlFilePath, FileMode.Truncate, FileAccess.Write)
       using(var writer = new TextWriter(fileStream))
       {
          foreach(var line in lines)
             writer.Write(line);
    
          writer.Flush();
       } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some special characters in my data inside xml.So i am getting error
Could someone help me understand why I'm getting this error: javax.xml.bind.UnmarshalException: unexpected element (uri:,
I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as
For some reason, I'm getting unexpected results in the range comparisons of unicode characters.
I am getting an unexpected Access Violation error in the following code: program Project65;
I'm getting the following error when trying to add some data from myXml.xml to
i am getting this error Unexpected error encountered while preparing for the operation. org.eclipse.core.runtime.AssertionFailException
I am getting this error Parse error : syntax error, unexpected T_STRING in /ho.../ssd/recording-goodbye.php
I'm getting the error: The exception message is 'The incoming message has an unexpected
I'm getting unexpected output from the all.equal method in R, specifically the implementation for

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.