Reputation: 60085
I have:
public class B
{
public string Some { get; set; }
}
public class D : B
{
public string More { get; set; }
}
[KnownType(typeof(D))]
public class X
{
public B[] Col { get; set; }
}
I want to automatically read/write XML exactly like:
<X>
<Col>
<B Some="val1" />
<D Some="val2" More="val3" />
</Col>
</X>
Neither XmlSerializer
not DataContractSerializer
helped me. This XML structure is mandatory.
So question is: can this be achieved or i have to parse that XML manually?
Thanks, Andrey
Upvotes: 4
Views: 3490
Reputation: 755249
It sounds like you're having trouble serializing the collection portion of the object. When serializing a collection in XML which can contain derived types, you need to inform the serializer about all of the derived types which could appear in the collection with the XmlInclude attribute
[KnownType(typeof(D))]
public class X
{
[XmlInclude(Type=typeof(B))]
[XmlInclude(Type=typeof(D))]
public B[] Col { get; set; }
}
Upvotes: 4
Reputation: 2237
Try XmlArrayItem with XmlSerializer:
public class X
{
[XmlArrayItem(typeof(D)),
XmlArrayItem(typeof(B))]
public B[] Col { get; set; }
}
Upvotes: 10