CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Xml serialization

    Hi,

    I am trying to hind a public property of a base class from the serialization of the derived class. But it does not work. Where is my mistake?

    Code:
    [XmlRoot("Components")
    public class Component
    {
      private List<Part> _parts = new List<Part>();
    
      [XmlElement("Parts")]
      public virtual List<Part> Parts
      {
        get { return _parts; }
        set { _parts = value; }
      }
    }
    
    [XmlRoot("Other_Components")]
    public class ComponentA : Component
    {
      [XmlIgnore]
      public override List<Part> Parts
      {
        get { return base.Parts; }
        set { base.Parts = value; }
      }
    }
    If I serialize ComponentA I do not want serialize Parts. But this code fails. Any hints?
    Useful or not? Rate my posting. Thanks.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Xml serialization

    I guess the [XmlElement] attribute is inherited by Parts in ComponentA. I suggest you try to declare Parts in ComponentA as new, not an override.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Xml serialization

    It is a MISTAKE to try to do this. It violates the IS-A policy. You should consider aggregation rather than inheritance.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Xml serialization

    @cilu: Your hint did not work too.

    @TheCPUWizard: I know that my design is not very well. But I did not found any good approach for my problem. I will try to explain my problem. Maybe you can show me the right direction.

    I have to design the following behavior and get this kinds of xml fragments:
    Code:
    <component hasPart="true">
      <parts>
        <part color="red" id="1" />
        <part color="blue" id="2" />
      </parts>
    <component>
    
    or
    
    <component hasParts="false" color="red" id="1" />
    To engage the problem I have created my class Component for the internal usage. If I get a component fragment without a part I will put regardless one instance of part into the parts collection. Inside of that part I store the properties for the component. If I get a component with parts I store each part into the parts collection as usual.

    The ComponentA class I use at the moment only to serialize the object in the right structure. I do not want to serialize each property manually.

    I am not happy with this approach but how I said I did not found a better one for the moment. A good advice is very welcomed.
    Last edited by torrud; May 30th, 2008 at 03:35 AM.
    Useful or not? Rate my posting. Thanks.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Xml serialization

    No need for anything fancy. Jusr check for an empty collection AFTER deserialization, and put in your instance.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured