A
Angelos Karantzalis
Hi guys. I've come across a problem when I tried to serialize a class into
xml, only to discover that the parent class's XML Serialization properties
weren't included in the output xml.
Actually, the class I'm serializing is two steps down in the inheritance
ladder. It's got a parent class which also has a parent class All those
classes in the hierarchy are Xml Serializable, and I'd think that it should
be obvious that all attributes/properties of the parents should be
serialized for any given subclass, no ?
Here's the two classes, subclass first, parent classes afterwards:
[XmlRootAttribute(Namespace="", ElementName="Group", IsNullable=false)]
public class Group : CBusLogicalObject
{
private Unit[] m_units = null;
public Group() : base()
{
}
public Group(string strAddress, CBusNode parent)
: base(strAddress, parent) {
}
public Group(string strAddress, string strName, CBusNode parent)
: base(strAddress, strName, parent) {
}
[XmlArrayItem(ElementName="units", Type=typeof(Unit))]
[XmlArray(ElementName="units")]
public Unit[] Units {
get { return m_units;}
set {
// TODO: Add some validation logic here ...
m_units = value;
}
}
}
... and the parent ...
[XmlRootAttribute(Namespace="", ElementName="LObject", IsNullable=false)]
public abstract class CBusLogicalObject : CBusNode
{
public CBusLogicalObject() : base()
{
}
public CBusLogicalObject(string strAddress, CBusNode parent)
: base(strAddress, parent){
m_address = parent.Address+"/"+strAddress;
}
public CBusLogicalObject(string strAddress, string strName, CBusNode parent)
: base(strAddress, strName, parent){
m_address = parent.Address+"/"+strAddress;
}
}
... and the paren't parent ( don't blame me ... )
[XmlRootAttribute(Namespace="", ElementName="CBusNode", IsNullable=false)]
public abstract class CBusNode
{
protected string m_address = null;
protected string m_name = null;
private CBusNode _parent = null;
public CBusNode()
{
}
public CBusNode(string address, CBusNode parent) {
this._parent = parent;
this.m_address = address;
}
public CBusNode(string address, string name, CBusNode parent) {
this._parent = parent;
this.m_address = m_address;
this.m_name = name;
}
[XmlAttributeAttribute(AttributeName="Address")]
public string Address {
get { return m_address; }
}
[XmlAttributeAttribute(AttributeName="Name")]
public string Name {
get {return m_name; }
}
Thanks a lot for any help,
Cheers,
Angel
O:]
xml, only to discover that the parent class's XML Serialization properties
weren't included in the output xml.
Actually, the class I'm serializing is two steps down in the inheritance
ladder. It's got a parent class which also has a parent class All those
classes in the hierarchy are Xml Serializable, and I'd think that it should
be obvious that all attributes/properties of the parents should be
serialized for any given subclass, no ?
Here's the two classes, subclass first, parent classes afterwards:
[XmlRootAttribute(Namespace="", ElementName="Group", IsNullable=false)]
public class Group : CBusLogicalObject
{
private Unit[] m_units = null;
public Group() : base()
{
}
public Group(string strAddress, CBusNode parent)
: base(strAddress, parent) {
}
public Group(string strAddress, string strName, CBusNode parent)
: base(strAddress, strName, parent) {
}
[XmlArrayItem(ElementName="units", Type=typeof(Unit))]
[XmlArray(ElementName="units")]
public Unit[] Units {
get { return m_units;}
set {
// TODO: Add some validation logic here ...
m_units = value;
}
}
}
... and the parent ...
[XmlRootAttribute(Namespace="", ElementName="LObject", IsNullable=false)]
public abstract class CBusLogicalObject : CBusNode
{
public CBusLogicalObject() : base()
{
}
public CBusLogicalObject(string strAddress, CBusNode parent)
: base(strAddress, parent){
m_address = parent.Address+"/"+strAddress;
}
public CBusLogicalObject(string strAddress, string strName, CBusNode parent)
: base(strAddress, strName, parent){
m_address = parent.Address+"/"+strAddress;
}
}
... and the paren't parent ( don't blame me ... )
[XmlRootAttribute(Namespace="", ElementName="CBusNode", IsNullable=false)]
public abstract class CBusNode
{
protected string m_address = null;
protected string m_name = null;
private CBusNode _parent = null;
public CBusNode()
{
}
public CBusNode(string address, CBusNode parent) {
this._parent = parent;
this.m_address = address;
}
public CBusNode(string address, string name, CBusNode parent) {
this._parent = parent;
this.m_address = m_address;
this.m_name = name;
}
[XmlAttributeAttribute(AttributeName="Address")]
public string Address {
get { return m_address; }
}
[XmlAttributeAttribute(AttributeName="Name")]
public string Name {
get {return m_name; }
}
Thanks a lot for any help,
Cheers,
Angel
O:]