XML 反序列化不反序列化元素
下午好,
我有以下类,
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 元素。它总是返回为空。有什么想法我做错了吗?所有其他元素似乎都正确反序列化。我从帖子中遗漏了不相关的课程。如果有人对我如何正确检索这个值有任何指示,我很乐意听到它。非常感谢您的帮助。
干杯,
〜ck在圣地亚哥
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未经测试,但它应该需要类似下面的类的东西。我不确定 XmlText 将如何处理整数。
Untested but it should need something like the class below. I'm not sure how XmlText will behave with an integer.