GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Validator
  • E-invoice
  • Schemas
  • XML formatter Pretty-print or minify XML
  • XML viewer A document as a tree you can search
  • XML to PDF Any XML as a readable PDF
  • XML to JSON Arrays where the XSD allows
  • XPath tester XPath 3.1, matches with their lines
  • JSON to XML @keys as attributes, arrays repeated
  • XSLT transformer XSLT 3.0 with SaxonJS
  • XML to CSV Invoice lines and records as rows
  • XML diff Compare two documents as trees
  • XSD generator An XML Schema from sample XML
  • XML from XSD A sample document from a schema

This view

  • Original page
  • New address

GeezerVison is showing xmlvalid.com — Unexpected XML declaration (.NET): how to fix | xmlvalid

Unexpected XML declaration

An XML declaration (<?xml …?>) appears somewhere other than the very start

The message, as .NET reports it
Unexpected XML declaration. The XML declaration must be the first node in the document, and no whitespace characters are allowed to appear before it. Line 2, position 3.

Why it happens

  • Whitespace or a blank line before <?xml, often from a verbatim string (@" followed by a line break) or a template.
  • Two documents concatenated.

How to fix it

Trim the input before parsing (xml.TrimStart()), or remove the second declaration when combining documents.

.NET throws an XmlSchemaValidationException at the first schema error unless XmlReaderSettings.ValidationEventHandler is set, which then receives every one with its line and position. A well-formedness error is an XmlException and ends reading whatever the settings.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as XML declaration allowed only at the start of the document. It checks the whole document in your browser and lists every schema error with its line and column, not only the first

XML declaration allowed only at the start of the document

Example

Reports the error

<?xml version="1.0"?>
<note/>
Fixed
<?xml version="1.0"?>
<note/>

Check your document

The validator lists every problem with its line and column, and links the ones explained here. It runs in your browser; nothing is uploaded

Open the validator

More .NET (System.Xml) errors

  • The element has invalid child element
  • The element has incomplete content
  • The element cannot contain text
  • The attribute is not declared
  • The required attribute is missing
  • The element is not declared
All error messages →
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.