【发布时间】:2021-11-12 03:44:42
【问题描述】:
我有这个 XML 的 sn-p(实际上是 XBRL,但那是基于 XML)
<xbrl>
<context id="Context_Duration" />
<context id="Context_Instant_Begin" />
<context id="Context_Instant_End" />
<ConstructionDepotSpecification>
<ConstructionDepotLabel contextRef="Context_Duration">depot</ConstructionDepotLabel>
<ConstructionDepotBalance contextRef="Context_Instant_Begin">45000</ConstructionDepotBalance>
<ConstructionDepotBalance contextRef="Context_Instant_End">42000</ConstructionDepotBalance>
</ConstructionDepotSpecification>
</xbrl>
(为清楚起见,删除了其他内容和 xml 命名空间声明)
我想将其反序列化为一个类,但我不确定如何处理 ConstructionDepotBalance 元素。如果我定义一个属性 ConstructionDepotBalance ,它将只取第一个元素的值,所以我认为我应该创建两个属性,一个用于开始值,一个用于结束值。 所以这个类应该是这样的
[XmlRoot(ElementName = "xbrl")]
public partial class Taxonomy
{
[XmlElement]
public List<ConstructionDepotSpecification> ConstructionDepotSpecification { get; set; }
}
public partial class ConstructionDepotSpecification
{
public string ConstructionDepotLabel { get; set; }
public long? ConstructionDepotBalanceBegin { get; set; }
public long? ConstructionDepotBalanceEnd { get; set; }
}
因此,具有 Context_Instant_Begin 属性的元素应反序列化为 ConstructionDepotBalanceBegin,而具有 Context_Instant_End 属性的其他元素应反序列化为 ConstructionDepotBalanceEnd。
这有可能实现吗?我应该为此使用 IXmlSerializable 实现吗?
【问题讨论】:
标签: c# xml xml-deserialization xbrl