c# - Deserialise XML File to .Net Object -


i'm trying deserialize xml file .net object doing like:

carcollection mycarcollection = null; string path = "carcollection.xml";  xmlserializer serializer = new xmlserializer(typeof(carcollection));  streamreader reader = new streamreader(path); mycarcollection= (carcollection)serializer.deserialize(reader); reader.close(); 

here xml file i'm using:

<?xml version="1.0" encoding="utf-8" ?> <carcollection>   <car id="a">     <cartype make="ford" model="focus" />     <carowner name="tom">       <report type="service">         <reportlist>           <date>20-08-2010</date>         </reportlist>       </report>     </carowner>   </car>   <car id="b">     <cartype make="vauxhall " model="corsa" />     <carowner name="joe">       <report type="service">         <reportlist>           <date>10-10-2008</date>           <date>10-10-2009</date>           <date>10-10-2010</date>                   </reportlist>      </report>       <report type="accident">         <reportlist>          <date>20-01-2011</date>         </reportlist>       </report>     </carowner>   </car> </carcollection> 

i've tried many things can't seem working.

could please me how deserialize .net object.

here c# objects

[serializable()] [xmlroot("carcollection")] public class carcollection {     [xmlarray("car")]     [xmlarrayitem("car", typeof(car))]     public car[] cars { get; set; } }  [serializable()] public class car {     [xmlattribute("make")]     public string carmaketype { get; set; }      [xmlattribute("model")]     public string carmodeltype { get; set; }      [xmlarray("carowner")]     [xmlarrayitem("carowner", typeof(carowner))]     public carowner[] carowners { get; set; } }  [serializable()] public class carowner {     [xmlattribute("name")]     public string name { get; set; }      [xmlarray("report")]     [xmlarrayitem("report", typeof(report))]     public report[] reports { get; set; } }  [serializable()] public class report {     [xmlattribute("type")]     public string type { get; set; }      [xmlarray("report")]     [xmlarrayitem("report", typeof(datetime))]     public datetime[] reports { get; set; } } 

tangentially might find benefit in using xsd generate xml classes.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -