Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a class deserialize as a different name

for instance something like:

<apple />

will serialize just fine to a class called "apple". however, if I want to call that class "Dragon" it will not serialize (which makes sense). I want to know how to mark up "Dragon" such that when the XmlSerializer sees it, it knows that "Dragon" is the same as

like image 608
LunchMarble Avatar asked Jan 15 '12 23:01

LunchMarble


2 Answers

Assuming Dragon defines at least a superset of the properties and fields that apple does then competent_tech's answer is appropriate though I think your question is actually asking about:

[System.Xml.Serialization.XmlType("apple")]
public class Dragon

If Dragon is not compatible with apple then you may be better off performing an explicit conversion between the types. Assuming your application knows the definition of both apple and Dragon, this can be accomplished by deserializing your apple stream to an apple object, mapping the properties to a new Dragon object, and then serializing your Dragon object.

like image 133
M.Babcock Avatar answered Nov 03 '22 00:11

M.Babcock


You would want to add the System.Xml.Serialization.XmlTypeAttribute to the class.

[System.Xml.Serialization.XmlType("Dragon")]
public class apple
like image 34
competent_tech Avatar answered Nov 03 '22 00:11

competent_tech