开发者

Deserialise XML File to .Net Object

开发者 https://www.devze.com 2023-02-06 10:03 出处:网络
I\'m trying to deserialize an xml file to a .NET object by doing something like: CarCollection myCarCollection = null;

I'm trying to deserialize an xml file to 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've tried many things but can't seem to get it working.

Could anyone please help me how to do deserialize to a .NET object.

Here is 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; }
}


Tangentially you might find some benefit in using XSD to generate XML from your classes.


I bet this is due to the date format. the xmlns declaration is also missing.

The Felice suggestion is a good one. Try to produce the desired result with serializing, before trying to deserialize


Read this page in the MSDN to verify your code. The yellow note halfway down the page specifies what requirements must be met by collections.

Also: pass the Car type too, to the Serializer constructor.

EDIT The Report and Car tags are not closed!

EDIT

Here is the output when serializing. Spot the differences there are many. The biggest problem is how you are serializing the arrays. Start using plurals (Cars, Owners) for collections that wil 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

精彩评论

暂无评论...
验证码 换一张
取 消