Problem
I'm using Xsd2Code (a .NET class generator for XSD schema) on a simple settings file schema. For some reason, when I attempt to use the built-in LoadFromFile()
or Deserialize()
methods, I get an exception that seems to be related to the xmlns
attributes in my XSD and XML files. If I remove these attributes, the exception goes away. (Please see my "Update" below for details on the code that is actually causing the exception.)
XSD File
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns="Myco.CLDatabaseBuilder.Models"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="Myco.CLDatabaseBuilder.Models"
elementFormDefault="qualified">
<xs:element name="Settings" type="Settings" />
<xs:complexType name="Settings">
<xs:sequence>
<xs:element name="SqlServerInstanceName" type="xs:string" />
<xs:element name="DatabaseName" type="xs:string" />
<xs:element name="RootDatabaseName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
XML File (fails) - I get an Exception when I attempt to deserialize
<?xml version="1.0" encoding="UTF-8"?>
<Settings
xmlns="Myco.CLDatabaseBuilder.Models"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Myco.CLDatabaseBuilder.Models Settings.xsd">
<SqlServerInstanceName>SQLEXPRESS</SqlServerInstanceName>
<DatabaseName>CL</DatabaseName>
<RootDatabaseName>master</RootDatabaseName>
</Settings>
XML File (succeeds) - deserialization works fine
<?xml version="1.0" encoding="UTF-8"?>
<Settings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Myco.CLDatabaseBuilder.Models Settings.xsd">
<SqlServerInstanceName>SQLEXPRESS</SqlServerInstanceName>
<DatabaseName>CL</DatabaseName>
<RootDatabaseName>master</RootDatabaseName>
</Settings>
Exception Details - what happens if I run the "failure example"
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: <Settings xmlns='Myco.CLDatabaseBuilder.Models'> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSettings.Read3_Settings()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at Myco.CLDatabaseBuilder.Models.Settings.Deserialize(String xml) in C:\...\CLDatabaseBuilder\Models\Settings.cs:line 118
at Myco.CLDatabaseBuilder.Models.Settings.LoadFromFile(String fileName) in C:\...\CLDatabaseBuilder\Models\Settings.cs:line 195
at Myco.CLDatabaseBuilder.Program.InitializeSettings() in C:\...\CLDatabaseBuilder\Program.cs:line 68
Questions:
So basically, I have to strip out the default namespace to be able to deserialize.
Any idea why I'm getting an exception when I use xmlns
? This namespace declaration is very helpful for doing live validation in Visual Studio, so I'd like to keep it if possible.
Is something wrong with my XSD root element attributes? I've messed with elementFormDefault
(changing from qualified
to unqualified
), but this didn't solve the problem.
Update
The code it's actually choking on is this:
Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Myco.CDDatabaseBuilder.Models.Settings));
Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader));
As shown in the exception above, the error I'm getting is:
<Settings xmlns='Myco.CLDatabaseBuilder.Models'>
was not expected.
Does this suggest anything to look for in my Settings
class (the class generated by Xsd2Code)? There are [System.Xml.Serialization...]
attributes sprinkled throughout this class. I wonder if something is missing, or if the class (or the serializer) doesn't have a way of modeling the default xmlns
attribute. Any ideas appreciated.