Mapping xml to classes C# -


how can deserialize , map following xml appropriate classes? after deserialization have 2 classes of type cat , dog.

<?xml version="1.0" encoding="utf-8"?> <arrayofanimal>   <animal type="cat">     <name>buddy</name>     <age>2</age>   </animal>   <animal type="dog">     <name>aman</name>     <age>12</age>   </animal> </arrayofanimal> 

classes cat , dog derived animal class , looks this:

    public class cat : animal     {     }      public class dog : animal     {     }      [xmlinclude(typeof(cat))]     [xmlinclude(typeof(dog))]     public abstract class animal     {         public string name{ get; set;  }         public string age { get; set; }     } 

right when serialize class cat , dog xml xsi attributes:

<?xml version="1.0" encoding="utf-8"?> <arrayofanimal xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <animal xsi:type="cat">     <name>buddy</name>     <age>2</age>   </animal>   <animal xsi:type="dog">     <name>aman</name>     <age>12</age>   </animal> </arrayofanimal> 

thank you.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -