locked
XmlSerialization and UserControl: Don't serialize inherited properties

    Question

  • I have a UserControl to which I have added several of my own properties. I want to serialize only the properties I have added (not the ones inherited from UserControl. I know how to use the Xml attributes (XmlRoot, XmlAttribute, XmlIgnore, etc.), but since I cannot add these attributes to the inherited properties, the XmlSerializer still tries to serialize them. I have considered creating an extra class that does not inherit from UserControl to use for storing the data separately or as a middleman when serializing and deserializing, but this seems like an extra step that should not be necessary. Is there any way to say "Don't serialize inherited properties" when using XmlSerializer, or is creating an extra class my only choice? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 29, 2014 5:07 PM

Answers

  • Yes, there is a way.

    When you create the XmlSerializer, you will have to tell it to explicitly ignore the field on the base class. It will ignore it for any object it finds of that type: 

    XmlAttributes attrs = new XmlAttributes(); 
    attrs.XmlIgnore = true; // you can add any attributes you want here; allowing you to 'customize' the BaseClass element 
    XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); 
    attrOverrides.Add(typeof(BaseClass), "Property1", attrs); 
    XmlSerializer xSerializer = new XmlSerializer(typeof(Patient), attrOverrides); 

    ... and now xSerializer will exclude property1 when it serializes any class inheriting from BaseClass (or BaseClass itself). 

    Mark as response if helpful.

    Wednesday, October 29, 2014 5:28 PM
  • You need to specify explicity the Properties to overide:

    attrOverrides.Add(typeof(BaseClass), "Property1", attrs); 

    Wednesday, October 29, 2014 6:56 PM

All replies

  • Yes, there is a way.

    When you create the XmlSerializer, you will have to tell it to explicitly ignore the field on the base class. It will ignore it for any object it finds of that type: 

    XmlAttributes attrs = new XmlAttributes(); 
    attrs.XmlIgnore = true; // you can add any attributes you want here; allowing you to 'customize' the BaseClass element 
    XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); 
    attrOverrides.Add(typeof(BaseClass), "Property1", attrs); 
    XmlSerializer xSerializer = new XmlSerializer(typeof(Patient), attrOverrides); 

    ... and now xSerializer will exclude property1 when it serializes any class inheriting from BaseClass (or BaseClass itself). 

    Mark as response if helpful.

    Wednesday, October 29, 2014 5:28 PM
  • I tried that using the following code:
    Dim xmlattr As New XmlAttributes()
    xmlattr.XmlIgnore = True
    Dim xmlover As New XmlAttributeOverrides()
    xmlover.Add(GetType(UserControl), xmlattr)
    Dim xml As New XmlSerializer(GetType(Card()), xmlover)

    But I still get a System.InvalidOperationException error (Card is the name of my class that inherits from UserControl). I noticed that my Add method does not specify a specific property, but if I needed to specify every property, would it be worth it? I figured that was the point of the overload I chose. Am I doing something wrong? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 29, 2014 6:43 PM
  • You need to specify explicity the Properties to overide:

    attrOverrides.Add(typeof(BaseClass), "Property1", attrs); 

    Wednesday, October 29, 2014 6:56 PM
  • OK, I guess that answers my question, but I don't think it solves my problem. If I have to do that for every property in UserControl, I don't think it's worth the extra code needed to include every property, because it would be easy to forget one, and to iterate them using reflection could greatly hurt performance. But I guess it's still a good feature to know about.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, October 29, 2014 8:15 PM
  • Nathan Sokalski, let's remember your question above "Is there any way to say "Don't serialize inherited properties" when using XmlSerializer, or is creating an extra class my only choice? "

    With the answer provided is clear that there is an alternative than creating extra classes, then your original question is answered. What's better? It's a technical decision that just you can take, considering your entire project and restrictions.

    Remember: when you mark a response, you are helping the next people with the same question how to solve their problems too.




    Thursday, October 30, 2014 1:44 AM