How to make a class deserialized as another name

for example, something like:

<apple /> 

will serialize just fine to a class called "apple." however, if I want to name this class β€œDragon”, it will not be serialized (which makes sense). I want to know how to mark up the Dragon so that when the XmlSerializer sees it, it knows that the Dragon matches

+6
source share
2 answers

Assuming Dragon defines at least a superset of the properties and fields that apple does, then the competent answer is consistent, although I think your question really asks:

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

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

+8
source

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

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

Source: https://habr.com/ru/post/906061/


All Articles