Use XmlAttributeOverrides, but get an InvalidOperationException nevertheless
-
Hello, I'd like to serialize an Objekt with XmlSerializer. The Objekt is a kind of treeview with lots of public properties like this:
public object Instance;
Currently I have different kind of classes stored in the Instance objects, all inheriting from the same base class. If I want to serialize these, I need to tell the XmlSerializer in an XmlAttributeOverrides object about the different Instance object classes. I collect this information dynamically before starting the XmlSerializer. Setting a breakpoint just before that I can check the Overrides object which looks like this:
- Overrides {System.Xml.Serialization.XmlAttributeOverrides} System.Xml.Serialization.XmlAttributeOverrides
-
Non-Public members
-
types Count = 5 System.Collections.Hashtable
-
\[{CTrModules.MDataSaveXML}\] {System.Collections.Hashtable}
-
Key {Name = "MDataSaveXML" FullName = "CTrModules.MDataSaveXML"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.QSM\_Var\_Dict}\] {System.Collections.Hashtable}
-
Key {Name = "QSM\_Var\_Dict" FullName = "CTrModules.QSM\_Var\_Dict"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{System.String}\] {System.Collections.Hashtable}
-
Key {Name = "String" FullName = "System.String"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.MHandleException}\] {System.Collections.Hashtable}
-
Key {Name = "MHandleException" FullName = "CTrModules.MHandleException"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{FFV\_Cockpit.InternalExcHandler}\] {System.Collections.Hashtable}
-
Key {Name = "InternalExcHandler" FullName = "FFV\_Cockpit.InternalExcHandler"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serializat
-
-
Hello, I'd like to serialize an Objekt with XmlSerializer. The Objekt is a kind of treeview with lots of public properties like this:
public object Instance;
Currently I have different kind of classes stored in the Instance objects, all inheriting from the same base class. If I want to serialize these, I need to tell the XmlSerializer in an XmlAttributeOverrides object about the different Instance object classes. I collect this information dynamically before starting the XmlSerializer. Setting a breakpoint just before that I can check the Overrides object which looks like this:
- Overrides {System.Xml.Serialization.XmlAttributeOverrides} System.Xml.Serialization.XmlAttributeOverrides
-
Non-Public members
-
types Count = 5 System.Collections.Hashtable
-
\[{CTrModules.MDataSaveXML}\] {System.Collections.Hashtable}
-
Key {Name = "MDataSaveXML" FullName = "CTrModules.MDataSaveXML"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.QSM\_Var\_Dict}\] {System.Collections.Hashtable}
-
Key {Name = "QSM\_Var\_Dict" FullName = "CTrModules.QSM\_Var\_Dict"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{System.String}\] {System.Collections.Hashtable}
-
Key {Name = "String" FullName = "System.String"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.MHandleException}\] {System.Collections.Hashtable}
-
Key {Name = "MHandleException" FullName = "CTrModules.MHandleException"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{FFV\_Cockpit.InternalExcHandler}\] {System.Collections.Hashtable}
-
Key {Name = "InternalExcHandler" FullName = "FFV\_Cockpit.InternalExcHandler"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serializat
Did you add the
[XmlInclude(typeof(CTrModules.MDataSaveXML))]
attribute to the base class or pass an array of types to the constructor of the serializer? See e.g. http://stackoverflow.com/questions/4923896/i-am-getting-an-error-when-trying-to-xml-serialize-an-object[^] -
-
Did you add the
[XmlInclude(typeof(CTrModules.MDataSaveXML))]
attribute to the base class or pass an array of types to the constructor of the serializer? See e.g. http://stackoverflow.com/questions/4923896/i-am-getting-an-error-when-trying-to-xml-serialize-an-object[^]Hi Bernhard,
Bernhard Hiller wrote:
Did you add the
[XmlInclude(typeof(CTrModules.MDataSaveXML))]
attribute to the base classI don't really want to go with that option, because then I have to remember to add such an include for any class I derive from the base class. But I tried it just for test purposes: I get the same problem. The option I chose to use is this one:
Bernhard Hiller wrote:
or pass an array of types to the constructor of the serializer?
I wrote a method to gather all types in an array and I pass this array to the serializer. I set a breakpoint at the call of the serializer and inspected this array:
- Overrides {System.Xml.Serialization.XmlAttributeOverrides} System.Xml.Serialization.XmlAttributeOverrides
-
Non-Public members
-
types Count = 5 System.Collections.Hashtable
-
\[{CTrModules.MDataSaveXML}\] {System.Collections.Hashtable}
-
Key {Name = "MDataSaveXML" FullName = "CTrModules.MDataSaveXML"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.QSM\_Var\_Dict}\] {System.Collections.Hashtable}
-
Key {Name = "QSM\_Var\_Dict" FullName = "CTrModules.QSM\_Var\_Dict"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{System.String}\] {System.Collections.Hashtable}
-
Key {Name = "String" FullName = "System.String"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{CTrModules.MHandleException}\] {System.Collections.Hashtable}
-
Key {Name = "MHandleException" FullName = "CTrModules.MHandleException"} object {System.RuntimeType}
-
Value Count = 1 object {System.Collections.Hashtable}
-
\["Instance"\] {System.Xml.Serialization.XmlAttributes}
-
Raw View
-
\[{FFV\_Cockpit.InternalExcHandler}\] {System.Collections.Hashtable}
-
Key {Name = "InternalExcHandler" FullName = "FFV\_Cockpit.InternalExcHandler&q
-