Problem
I am using Xsd2Code (the .NET class generator for the XSD scheme) in a simple settings file scheme. For some reason, when I try 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 will disappear. (For more information about the code that throws the exception, see my Refresh .)
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 (failed) . I get an exception when trying 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 (successful) - 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 "crash 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 remove the default namespace in order to be able to deserialize.
Any idea why I get an exception when I use xmlns ? . This namespace declaration is very useful for performing active validation in Visual Studio, so I would like to keep it if possible.
Is there something wrong with my XSD root element attributes? I mixed up with elementFormDefault (changing from qualified to unqualified ), but this did not solve the problem.
Update
The code that he really chokes 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 get is:
<Settings xmlns='Myco.CLDatabaseBuilder.Models'> not expected.
Does this suggest looking for something in my Settings class (the class generated by Xsd2Code)? This class has the attributes [System.Xml.Serialization...] . Interestingly, something is missing, or if the class (or serializer) does not have a way to model the default xmlns attribute. Any ideas appreciated.