【问题标题】:Xml Deserialization - Mapping Xml Attribute to Property?Xml 反序列化 - 将 Xml 属性映射到属性?
【发布时间】:2023-04-07 15:08:01
【问题描述】:

如何将一组 Xml 元素(所有元素名称相同,但具有特定属性)反序列化为一个对象?

这是 Xml 的 sn-p:

<mibscalar name="lotOccupiedPct" link="http://localhost:8080/v1/mib/objs/lotOccupiedPct?type=xml">
<data index="1" value="0" counter="0"/>
</mibscalar>
<mibscalar name="gateStatusCloseThreshold" type="readonly" link="http://localhost:8080/v1/mib/objs/gateStatusCloseThreshold?type=xml">
<data index="1" value="90" counter="0"/>
</mibscalar>
<mibscalar name="gateStatusOpenThreshold" type="readonly" link="http://localhost:8080/v1/mib/objs/gateStatusOpenThreshold?type=xml">
<data index="1" value="70" counter="0"/>
</mibscalar>

以及我班级中的相应属性(按顺序):

public int CurrentOccupancyPercentage { get; set; }
public int CloseThresholdPercentage { get; set; }
public int OpenThresholdPercentage { get; set; }

我假设有一些属性可以添加到可以处理这种情况的属性中?

提前致谢。

【问题讨论】:

    标签: c# xml deserialization xml-deserialization


    【解决方案1】:

    您需要使用XmlAttributeXmlElement 构建一个具有属性的类。它将允许您序列化和反序列化为 XML 文件。

    这是一个例子Serialize Property as Xml Attribute in Element

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2021-11-20
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      相关资源
      最近更新 更多