Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Use XmlAttributeOverrides, but get an InvalidOperationException nevertheless

Use XmlAttributeOverrides, but get an InvalidOperationException nevertheless

Scheduled Pinned Locked Moved XML / XSL
debuggingxmljson
3 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian Treffler
    wrote on last edited by
    #1

    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
      
    B 1 Reply Last reply
    0
    • C Christian Treffler

      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
        
      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      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[^]

      C 1 Reply Last reply
      0
      • B Bernhard Hiller

        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[^]

        C Offline
        C Offline
        Christian Treffler
        wrote on last edited by
        #3

        Hi Bernhard,

        Bernhard Hiller wrote:

        Did you add the [XmlInclude(typeof(CTrModules.MDataSaveXML))] attribute to the base class

        I 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
          
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups