Deserialise XML文件.NET对象对象、文件、Deserialise、XML

2023-09-07 09:30:47 作者:我們的班主任妳威武雄壯!

我试图做类似反序列化XML文件.NET对象:

  CarCollection myCarCollection = NULL;
字符串路径=CarCollection.xml;

XmlSerializer的序列化=新的XmlSerializer(typeof运算(CarCollection));

StreamReader的读者=新的StreamReader(路径);
myCarCollection =(CarCollection)serializer.Deserialize(读卡器);
reader.Close();
 

下面是我使用的XML文件:

 < XML版本=1.0编码=UTF-8&GT?;
< CarCollection>
  <汽车ID =A>
    < CarType使=福特模型=焦点访谈/>
    < CarOwner名称=汤姆>
      <报告类型=服务>
        < ReportList>
          <日期> 20-08-2010< /日期>
        < / ReportList>
      < /报告>
    < / CarOwner>
  < /汽车>
  <汽车ID =B>
    < CarType使=沃克斯豪尔模型=可赛/>
    < CarOwner名=乔>
      <报告类型=服务>
        < ReportList>
          <日期> 2008年10月10号< /日期>
          <日期>&09-18-2009 LT; /日期>
          <日期> 2010年10月10日< /日期>
        < / ReportList>
     < /报告>
      <报告类型=意外>
        < ReportList>
         <日期> 20-01-2011< /日期>
        < / ReportList>
      < /报告>
    < / CarOwner>
  < /汽车>
< / CarCollection>
 

我尝试过很多事情,但似乎无法得到它的工作。

任何人都可以请帮我如何做反序列化到.NET对象。

百分点大数据技术团队 低代码平台实践

下面是C#对象

  [序列化()]
[XmlRoot(CarCollection)]
公共类CarCollection
{
    [XmlArray(汽车)]
    [XmlArrayItem(汽车的typeof(轿车))]
    大众汽车[]汽车{获得;组; }
}

[序列化()]
公共类车
{
    [XmlAttribute(使)
    公共字符串CarMakeType {获得;组; }

    [XmlAttribute(模型)
    公共字符串CarModelType {获得;组; }

    [XmlArray(CarOwner)]
    [XmlArrayItem(CarOwner的typeof(CarOwner))]
    公共CarOwner [] CarOwners {获得;组; }
}

[序列化()]
公共类CarOwner
{
    [XmlAttribute(名称)]
    公共字符串名称{;组; }

    [XmlArray(报告)]
    [XmlArrayItem(报告中的typeof(报告))]
    公众报告[]报告{获得;组; }
}

[序列化()]
公共类报告
{
    [XmlAttribute(类型)
    公共字符串类型{获取;组; }

    [XmlArray(报告)]
    [XmlArrayItem(报告中的typeof(DateTime的))]
    公共DateTime的[]报告{获得;组; }
}
 

解决方案

切圆您可能会发现在使用的 XSD 从你的类生成XML。

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.