开发者

XML Deserialization not deserializing element

开发者 https://www.devze.com 2023-02-09 08:27 出处:网络
Good Afternoon, I have the following classes public class MaintenanceBundle { [XmlAttribute(AttributeName = \"Required\")]

Good Afternoon,

I have the following classes

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

I'm trying to deserialize this xml to objects using these classes. Here is the 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>

For some reason I am unable to get the Mileage element inside the MntReason element. It keeps coming back as null. Any ideas what I am doing wrong? All the other elements seem to deserialize properly. I left out irrelevant classes from my post. If anyone has any pointers how I can prope开发者_运维知识库rly retreive this value I would love to hear it. Thanks so much for any help.

Cheers,

~ck in San Diego


Untested but it should need something like the class below. I'm not sure how XmlText will behave with an integer.

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

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

精彩评论

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