Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: The XML declaration must be the first node in the document

Tags:

c#

.net

xml

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"?>
-----
like image 983
user841311 Avatar asked Aug 14 '12 19:08

user841311


4 Answers

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();
   } 
like image 117
KeithS Avatar answered Nov 20 '22 00:11

KeithS


That is not valid XML.

As the error clearly states, the XML declaration (<?xml ... ?>) must come first.

like image 44
SLaks Avatar answered Nov 20 '22 00:11

SLaks


I'm using the following function to remove whitespace from xml:

public static void DoRemovespace(string strFile)
    {
        string str = System.IO.File.ReadAllText(strFile);
        str = str.Replace("\n", "");
        str = str.Replace("\r", "");
        Regex regex = new Regex(@">\s*<");
        string cleanedXml = regex.Replace(str, "><");
        System.IO.File.WriteAllText(strFile, cleanedXml);

    }
like image 43
Misheeta Avatar answered Nov 19 '22 22:11

Misheeta


Don't put any comments in the beginning of your file!

like image 1
MacGyver Avatar answered Nov 19 '22 22:11

MacGyver