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>

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Disappointing that this is how you have to do it but the correct answer so have some kudos :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.