Hello
I suspect the answer is going to be no, but...
I have a class that contains a property defined as an abstract class.
I would like to control the format of the generated XML to include a
container element that isn't actually implemented in my class.
This is best described by an example.
I have an abstract class that represents an expression:
public abstract class Expression {...}
I have extended this abstract class with various types of expression,
e.g.
public class Literal : Expression {...}
public class Field : Expression {...}
public class Operation : Expression {...}
I make use of this abstract class in another class, exposing it as a
property:
public class Comparison
{
private Expression _argument1;
private Expression _argument2;
public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}
public Expression Argument2
{
get {return _expression;}
set {_expression = value;}
}
}
To prepare the class for serialization, I decorate it using
[Serializable] and adding [XmlElement] attributes to the public
properties:
[Serializable]
public class Comparison
{
private Expression _argument1;
private Expression _argument2;
[XmlElement("literal", typeof(Literal))]
[XmlElement("field", typeof(Field))]
[XmlElement("operation", typeof(Operation))]
public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}
[XmlElement("literal", typeof(Literal))]
[XmlElement("field", typeof(Field))]
[XmlElement("operation", typeof(Operation))]
public Expression Argument2
{
get {return _expression;}
set {_expression = value;}
}
}
When argument 1 is a field and argument 2 is a literal (for example),
this generates XML of the form
<Comparison>
<field>
--field elements
</field>
<literal>
--literal elements
</literal>
</Comparison>
However, what I really want is XML of the form:
<Comparison>
<argument1>
<field>
--field elements
</field>
</argument1>
<argument2>
<literal>
--literal elements
</literal>
</argument2>
</Comparison>
So my question is: can this be achieved without physically
implementing a container class to represent the argument elements?
i.e. I don't want to have to change my class model by introducing a new
class.
Thanks very much
Sandy