Why should I remove the xmlns attribute to deserialize my XML file?

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.

+4
source share
2 answers

One of these days ... it turned out I need four little characters.

There is a switch in Xsd2Code /xa+ that sets GenerateXMLAttributes = true

For some reason, the default value for this value is incorrect. Well, that should be true if you have the xmlns attribute and you want to deserialize.

+8
source

I don’t know what the problem is, but it should work. You are right to be embarrassed. I checked your circuit using the xsd.exe tool that comes with the .NET SDK. It generates this class from your schema:

 [XmlType(Namespace="Myco.CLDatabaseBuilder.Models")] [XmlRoot(Namespace="Myco.CLDatabaseBuilder.Models", IsNullable=false)] public partial class Settings { private string sqlServerInstanceNameField; private string databaseNameField; private string rootDatabaseNameField; /// <remarks/> public string SqlServerInstanceName { get { return this.sqlServerInstanceNameField; } set { this.sqlServerInstanceNameField = value; } } /// <remarks/> public string DatabaseName { get { return this.databaseNameField; } set { this.databaseNameField = value; } } /// <remarks/> public string RootDatabaseName { get { return this.rootDatabaseNameField; } set { this.rootDatabaseNameField = value; } } } 

As you can see, there are several attributes that adorn the class. There XmlType and XmlRoot, and what it is. The three properties of this class are implicitly serialized, even without an XmlElement attribute.

The result obtained from re- serializing your XML:

 <q1:Settings xmlns:q1="Myco.CLDatabaseBuilder.Models"> <q1:SqlServerInstanceName>SQLEXPRESS</q1:SqlServerInstanceName> <q1:DatabaseName>CL</q1:DatabaseName> <q1:RootDatabaseName>master</q1:RootDatabaseName> </q1:Settings> 
+2
source

Source: https://habr.com/ru/post/1299044/


All Articles