1

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 properly retreive this value I would love to hear it. Thanks so much for any help.

Cheers,
~ck in San Diego

StaxMan
  • 113,358
  • 34
  • 211
  • 239
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
  • 1
    Your Mileage element is complex (it has a `Unit` attribute) - that might be it. – Oded Feb 09 '11 at 22:40
  • It seems like a lot of folks have run into issues dealing with Nullable types and XML Serialization. This post seems relevant: http://stackoverflow.com/questions/2074240/serializing-a-nullabledatetime-in-to-xml – John Wigger Feb 09 '11 at 22:56

1 Answers1

3

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;}
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139