views:

60

answers:

1

I have a class with two properties for max and min values. It looks like this (ish):

public class Configuration
{
  public int Max { get; set; }
  public int Min { get; set; }
}

When I serialize this I get something like:

<Configuration>
  <Max>10</Max>
  <Min>0</Min>
</Configuration>

However, I need an extra element like this:

<Configuration>
  <Bounds>
    <Max>10</Max>
    <Min>0</Min>
  </Bounds>
</Configuration>
+3  A: 

To do that you would need to introduce an extra layer into the object model, too. XmlSerializer likes the xml to be (roughly) a direct map to the objects:

[Serializable]
public class Configuration {
    public Bounds Bounds { get; set; }
}
[Serializable]
public class Bounds {
    public int Min {get;set;}
    public int Max {get;set;}
}

The only other option is to implement IXmlSerializable, but you really don't want to do that unless you absolutely have no choice.

Marc Gravell
+1 Beat me to this. :)
Michael G
Disappointing that this is how you have to do it but the correct answer so have some kudos :)