【问题标题】:Deserialise XML File to .Net Object将 XML 文件反序列化为 .Net 对象
【发布时间】:2011-01-27 06:13:21
【问题描述】:

我正在尝试通过执行以下操作将 xml 文件反序列化为 .NET 对象:

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();

这是我正在使用的 xml 文件:

<?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>

我尝试了很多东西,但似乎无法正常工作。

谁能帮助我如何反序列化为 .NET 对象。

这里是 C# 对象

[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; }
}

【问题讨论】:

  • 请张贴为什么它不工作,错误和什么不工作。还将代码发布到 CarCollection 类
  • 请告诉我们您对 Car 对象的定义。另外,它做错了什么。是否抛出任何异常?
  • 您是如何创建 CarCollection 类的?它是从 XML 模式派生的吗?手工编码?还有什么?
  • 您需要粘贴Car和CarCollection的代码
  • 另一个测试是创建一个 CarCollection 并对其进行序列化,检查它是否与您尝试反序列化的 xml 兼容

标签: c# .net xml serialization


【解决方案1】:

您可能会发现使用 XSD 从您的类生成 XML 的一些好处。

【讨论】:

  • 我已经尝试过了。我收到以下错误:无法生成临时类(结果 = 1)。错误 CS0030:无法将类型“CarCollectionCarCarCarOwnerReportReportListDate[]”转换为“CarCollectionCarCarCarOwnerReportReportListDate”错误 CS0029:无法将类型“CarCollectionCarCarOwnerReportReportListDate”隐式转换为“CarCollectionCarCarCarOwnerReportReportListDate[]”
  • 我发现了我在使用 XSD 时遇到的问题。我还没有使用该工具解决问题。谢谢
  • 很高兴听到你在路上 -- 抱歉,我一直在忙,没来得及回复。
【解决方案2】:

我敢打赌这是由于日期格式。 xmlns 声明也丢失了。

Felice 的建议很好。在尝试反序列化之前尝试通过序列化产生所需的结果

【讨论】:

  • 我不确定如何对对象进行序列化。到目前为止,我只是得到空对象。代码将编译并运行,但我最终得到空汽车对象。
【解决方案3】:

Read this page in the MSDN 验证您的代码。 页面中间的黄色注释指定集合必须满足哪些要求。

另外:将 Car 类型也传递给 Serializer 构造函数。

编辑 报告和汽车标签未关闭!

编辑

这是序列化时的输出。发现差异有很多。最大的问题是如何序列化数组。开始使用复数形式(Cars、Owners)来表示集合,使其更具可读性。

<?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>

【讨论】:

  • 相对于未封闭的标签:这可能是因为 OP 错误地格式化了降价;我试图解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
相关资源
最近更新 更多