14

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"?>
-----
user841311
  • 597
  • 3
  • 9
  • 24
  • 5
    I believe that `...?>` should be before the comments. Commenting syntax is part of the XML specification – Andre Calil Aug 14 '12 at 19:23
  • Is there a way to edit the xml file such that the defintion comes first and then the comments using c# so that I can load the xml document for further processing.. – user841311 Aug 14 '12 at 19:42
  • This is from IEX isn't it? I just ran into the same thing. Frustrating. – Jacob Lambert Aug 27 '15 at 15:26

4 Answers4

19

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();
   } 
KeithS
  • 70,210
  • 21
  • 112
  • 164
  • Yes you're right, the comments should come only after the xml definition but I get all xml files in that format and I have to load xml. Is there a way to edit the xml doc so that – user841311 Aug 14 '12 at 19:37
  • Yes, you would have to open the file using an ordinary FileStream, read it with a TextReader to extract the raw SQL, rearrange the lines of the file so the `` tag is first, and then write it back out, before pulling it in with the XmlReader. – KeithS Aug 14 '12 at 19:45
  • Thanks Keith! I've already tried your suggestion but couldn't succeed in re-arranging the lines of the file so if you can help me with that code it would be of great help to me. – user841311 Aug 14 '12 at 20:00
  • Perfect Keith! It's working like charm! Many thanks for the quick response while others just ignored it. – user841311 Aug 14 '12 at 20:26
5

That is not valid XML.

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    I can not control what's there in the xml file. I understand that it is not a valid xml according to the standards but I need to load the given xml using c# for further processing. – user841311 Aug 14 '12 at 19:34
1

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

    }
MichaelS
  • 5,941
  • 6
  • 31
  • 46
Misheeta
  • 51
  • 5
1

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

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245