[Mono-dev] XML validation : xsi:nil attribute ignored
yproust
yohann.proust at egis.fr
Fri Jan 23 06:34:16 EST 2009
Hi
I am trying to validate XML with an XSD schema, but it seems that the
xsi:nil attributes are ignored. As the element must be a dateTime,
validation fails on an empty element :
XmlSchema error: Invalidly typed data was specified. XML Line 3, Position
31.
I am using Mono 2.2 under CentOS 5.2.
The validation fails through Monodevelop 2.0 Alpha 2 (1.9.1) with the same
error.
On the other hand, the XML is validated with Microsoft Visual Studio under
Windows XP.
Are there specific options to use in the code ? A specific namespace to use
in the XSD and/or XML ?
Thanks for your help
Here are the files I used to perform my tests :
XSD
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://www.test.com" elementFormDefault="qualified"
targetNamespace="http://www.test.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Message">
<xs:complexType>
<xs:all>
<xs:element name="MyDateTime" nillable="true" type="xs:dateTime" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
Validated XML
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.test.com" xsi:schemaLocation="http://www.test.com
test.xsd">
<MyDateTime>2009-01-23T12:00:00Z</MyDateTime>
</Message>
Not validated XML
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.test.com" xsi:schemaLocation="http://www.test.com
test.xsd">
<MyDateTime xsi:nil="true"></MyDateTime>
</Message>
Code used to validate XML :
StreamReader reader = new StreamReader("test.xml");
string xml = reader.ReadToEnd();
string xsd = "/home/yproust/dev/test/test.xsd";
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs e)
{
throw new Exception("Error while validating message with xsd: " +
e.Message);
};
settings.ValidationType = ValidationType.Schema;
// Load the xsd
if( xsd != null )
settings.Schemas.Add(null, new XmlTextReader(xsd));
StringReader stringReader = new StringReader(message);
try
{
myXmlValidatingReader = XmlReader.Create(stringReader, settings);
while (myXmlValidatingReader.Read()) { }
myXmlValidatingReader.Close();
}
finally
{
stringReader.Close();
}
Error when validation fails :
XmlSchema error: Invalidly typed data was specified. XML Line 3, Position
31.
--
View this message in context: http://www.nabble.com/XML-validation-%3A-xsi%3Anil-attribute-ignored-tp21622901p21622901.html
Sent from the Mono - Dev mailing list archive at Nabble.com.
More information about the Mono-devel-list
mailing list