Validating XML against XSD using C#: Data at the root level is invalid.
Line 1, position 1
I am trying to validate an XML file against a XSD Schema, and am
encountering an XmlException, message "Data at the root level is invalid.
Line 1, position 1."
Based on a trawl through previous similar posts I have done the following:
Ensured that there is no whitespace at the start of the files.
Validated the XML and Schema at
http://www.corefiling.com/opensource/schemaValidate.html
Ensured my files are encoded without BOM.
Double-checked the absence of BOM in a hex editor. First three characters
of the XSD are 3C 3F 78, and the first three characters of the XML are
also 3C 3F 78.
Checked my code to ensure I was loading the XML using Load() and not
LoadXml()
But the error persists.
My XML loading function reads:
public void LoadXml(ref XmlDocument target, string path)
{
try
{
target = new XmlDocument();
target.Load(path);
}
catch (XmlException ex)
{
// Exception is thrown is there is an error in the Xml
MessageBox.Show(string.Format("An XML Exception was thrown
while loading the XML file from {0}.\nException text: {1}\nXML
file line: {2}", path, ex.Message, ex.LineNumber));
Application.Exit();
}
catch (Exception ex)
{
// General exception
MessageBox.Show(string.Format("A General Exception was thrown
while loading the XML file from {0}.\nException text: {1}",
path, ex.Message));
Application.Exit();
}
}
And my validation function:
private bool ValidateXml(string xmlPath, string schemaPath)
{
try
{
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ValidationEventHandler +=
XmlReaderValidationEventHandler;
xmlReaderSettings.CloseInput = true;
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.Schemas.Add(null, Settings.Default.SchemaPath);
xmlReaderSettings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints
|
XmlSchemaValidationFlags.ProcessInlineSchema
|
XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader sr = new StringReader(Settings.Default.PlcXmlPath);
using (XmlReader validatingReader = XmlReader.Create(sr,
xmlReaderSettings))
{
while (validatingReader.Read())
{
// Loop through the document
}
}
return true;
}
catch (XmlSchemaValidationException ex)
{
MessageBox.Show(string.Format("Unable to validate XML file {0}
against schema {1}\nException text: {2}\nXML Line: {3}\nData:
{4}", xmlPath, schemaPath, ex.Message, ex.LineNumber,
ex.Data));
return false;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Unable to validate XML file {0}
against schema {1}\nException text: {2}", xmlPath, schemaPath,
ex.Message));
return false;
}
}
I have taken my XSD and XML files all the way down to pretty much nothing,
but this error remains. Here is the XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MachineParameters">
</xsd:element>
</xsd:schema>
And here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<MachineParameters>Test
</MachineParameters>
Can anyone spot what I've done wrong, or suggest any further steps to try?
I would really appreciate your help - I have been bashing my head against
this all day.
No comments:
Post a Comment