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

发布于 2024-10-16 20:17:08 字数 2109 浏览 2 评论 0原文

下午好,

我有以下类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-10-23 20:17:08

未经测试,但它应该需要类似下面的类的东西。我不确定 XmlText 将如何处理整数。

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

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

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;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文