XML反序列化不反序列化元素

时间:2011-02-09 22:27:48

标签: c# xml serialization xml-serialization xmlserializer

下午好,

我有以下课程

public class MaintenanceBundle
{
    [XmlAttribute(AttributeName = "Required")]
    public string Required { get; set; }

    [XmlAttribute(AttributeName = "ID")]
    public string Id { get; set; }

    [XmlElement(ElementName = "Title")]
    public string Title { get; set; }

    [XmlElement(ElementName = "MntReason")]
    public MaintenanceReason Reason { get; set; }

    [XmlElement(ElementName = "Tasks")]
    public MaintenanceBundleCollection Tasks { get; set; }
}

public class MaintenanceBundleCollection
{
    [XmlElement(ElementName = "Task")]
    public List<MaintenanceBundleTask> Tasks { get; set; }
}

public class MaintenanceReason
{
    [XmlAttribute(AttributeName = "Every")]
    public string Every { get; set; }

    [XmlElement(ElementName = "Mileage", IsNullable = true)]
    public int? Mileage { get; set; }

    [XmlElement(ElementName = "Time", IsNullable = true)]
    public TimeInterval TimeInterval { get; set; }
}

我正在尝试使用这些类将此xml反序列化为对象。这是XML

<MntBundle Required="Yes" ID="S08870641702009101200000">
    <Title>DIRT OR DUSTY ROADS - 5000 MILES / 6 MONTHS</Title>
    <MntReason Every="No">
      <Mileage Unit="MILES">5000</Mileage>
    </MntReason>
    <Tasks>
      <Task ID="4-2" />
      <Task ID="4-3">
        <NMVCQualifier>Drive Shaft Boots</NMVCQualifier>
        <MVCQualifiers>
          <Qualifier Name="Drive Type">4WD</Qualifier>
        </MVCQualifiers>
      </Task>
      <Task ID="4-1" />
      <Task ID="4-4" />
      <Task ID="5-1">
        <MVCQualifiers>
          <Qualifier Name="Drive Type">4WD</Qualifier>
        </MVCQualifiers>
      </Task>
      <Task ID="6-1" />
      <Task ID="7-1" />
    </Tasks>
  </MntBundle>

由于某些原因,我无法在MntReason元素中获取Mileage元素。它一直回来为null。我有什么想法我做错了吗?所有其他元素似乎都适当地反序列化。我从帖子中遗漏了不相关的课程。如果有人有任何指示我怎么能正确地检索这个值我会很乐意听到它。非常感谢您的帮助。

干杯,
〜在圣地亚哥

1 个答案:

答案 0 :(得分:3)

未经测试,但它应该需要类似下面的课程。我不确定XmlText将如何表示整数。

public class Mileage
{
   [XmlAttribute(AttributeName = "Unit")]
   public string Unit {get; set;}

   [XmlText]
   public int Mileage {get; set;}
}