Hi I've recently been trying out XML serialization via the following MSDN article: http://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.71).aspx
Specifically, where it sais: "Serializing Derived Classes".
As you can see my code is very similar:
How many pink elephants do you see?
eight
2
nine
3
[Serializable]
public class Question2
{
protected ArrayList _answers;
public Question2()
{
_answers = new ArrayList();
}
[XmlArray(ElementName = "answers")]
[XmlArrayItem(ElementName = "answer", Type = typeof(Base)), XmlArrayItem(Type = typeof(Derived))]
public ArrayList Answers
{
get { return _answers; }
set { _answers = value; }
}
}
[Serializable]
public class Base
{
private string _answer;
[XmlElement(ElementName = "text", DataType = "string")]
public string AnswerString
{
get { return _answer; }
set { _answer = value; }
}
}
[Serializable]
public class Derived : Base
{
ushort _maleModifier;
[XmlElement(ElementName = "male", DataType = "unsignedShort")]
public ushort MaleModifier
{
get { return _maleModifier; }
set { _maleModifier = value; }
}
}
I've run into a very specific problem. Almost everything works fine, I'm able to deserialize the XML into my container class (not present),
but i'm not getting any derived info, only base.
If I reverse the order for the following line of code it works:
[XmlArrayItem(ElementName = "answer", Type = typeof(Base)), XmlArrayItem(Type = typeof(Derived))]
to:
[XmlArrayItem(ElementName = "answer", Type = typeof(Derived)), XmlArrayItem(Type = typeof(Base))]
I can remove Type = typeof(base) and that also works. But I don't want to do either of these because I want to separate my class structure from the XML. Plus, according to MSDN:
"Another use of the XmlArrayItemAttribute is to allow the serialization of derived classes. For example, another class named Manager that derives from Employee can be added to the previous example. If you do not apply the XmlArrayItemAttribute, the code will fail at run time because the derived class type will not be recognized. To remedy this, apply the attribute twice, each time setting the XmlArrayItemAttribute.Type property for each acceptable type (base and derived)."
I've done that. Apparently there's more to it. Any help/explanation would be appreciated.
Specifically, where it sais: "Serializing Derived Classes".
As you can see my code is very similar:
[Serializable]
public class Question2
{
protected ArrayList _answers;
public Question2()
{
_answers = new ArrayList();
}
[XmlArray(ElementName = "answers")]
[XmlArrayItem(ElementName = "answer", Type = typeof(Base)), XmlArrayItem(Type = typeof(Derived))]
public ArrayList Answers
{
get { return _answers; }
set { _answers = value; }
}
}
[Serializable]
public class Base
{
private string _answer;
[XmlElement(ElementName = "text", DataType = "string")]
public string AnswerString
{
get { return _answer; }
set { _answer = value; }
}
}
[Serializable]
public class Derived : Base
{
ushort _maleModifier;
[XmlElement(ElementName = "male", DataType = "unsignedShort")]
public ushort MaleModifier
{
get { return _maleModifier; }
set { _maleModifier = value; }
}
}
I've run into a very specific problem. Almost everything works fine, I'm able to deserialize the XML into my container class (not present),
but i'm not getting any derived info, only base.
If I reverse the order for the following line of code it works:
[XmlArrayItem(ElementName = "answer", Type = typeof(Base)), XmlArrayItem(Type = typeof(Derived))]
to:
[XmlArrayItem(ElementName = "answer", Type = typeof(Derived)), XmlArrayItem(Type = typeof(Base))]
I can remove Type = typeof(base) and that also works. But I don't want to do either of these because I want to separate my class structure from the XML. Plus, according to MSDN:
"Another use of the XmlArrayItemAttribute is to allow the serialization of derived classes. For example, another class named Manager that derives from Employee can be added to the previous example. If you do not apply the XmlArrayItemAttribute, the code will fail at run time because the derived class type will not be recognized. To remedy this, apply the attribute twice, each time setting the XmlArrayItemAttribute.Type property for each acceptable type (base and derived)."
I've done that. Apparently there's more to it. Any help/explanation would be appreciated.
VulpesPosted Dec 19, 2011, 12:58 PM
If I alter your Question2 class a bit, I'm able to get something similar (though not quite the same) as your XML file:
This gives the following XML file:
If I now reverse the two lines you mentioned, the file looks like this:
So, in both cases, the property which Derived is inheriting from Base and Derived's own property are being serialized.
Are you saying that this isn't the case when you do it?