Delete XML file in .Net object

I am trying to deserialize an xml file for a .NET object by doing something 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 is the 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 tried a lot of things but can't make it work.

Can someone please help me how to do deserialize for a .NET object.

Here are the 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; } } 
+1
c # xml serialization
source share
3 answers

You can potentially find the advantage of using XSD to generate XML from your classes.

+2
source share

I am sure this is due to the date format. The xmlns declaration is also missing.

Felice's suggestion is good. Try creating the desired serialization result before trying to deserialize

+2
source share

Read this page on MSDN to verify your code. A yellow note on half of the page indicates which requirements should be met by the collections.

Also: also pass the Car type to the Serializer constructor.

EDIT Report and Car tags are not closed!

EDIT

Here is the result of serialization. Identify the differences there are many. The biggest problem is how you serialize arrays. Start using plurals (Cars, Owners) for collections that will make it more readable.

 <?xml version="1.0" encoding="utf-8"?> <CarCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Car> <Car Make="make"> <CarOwner> <CarOwner Name="name1"> <Report> <Report Type="rtype"> <Report> <Report>2011-01-25T15:22:52.703125+01:00</Report> </Report> </Report> </Report> </CarOwner> </CarOwner> </Car> <Car Make="make2"> <CarOwner> <CarOwner Name="name3"> <Report> <Report Type="rtype"> <Report> <Report>2011-01-25T15:22:52.703125+01:00</Report> </Report> </Report> </Report> </CarOwner> </CarOwner> </Car> </Car> 

0
source share

All Articles