如何读取属性类型中的数字

本文关键字:类型 数字 属性 何读取 读取 | 更新日期: 2023-09-27 18:35:28

有人可以告诉我如何读取此XML中的数字20.0吗?我在XML.LinQ中使用c#和XElements。

<attribute id="Width" unit="mm" type="float">20.0</attribute> 

所以这个属性是一个子元素,当我写

gear.Attributes[0].Type.Value;

我得到"浮动"

我像这样定义"类型"

Type = el.Attribute("type").ToString();

如何读取属性类型中的数字

您应该

以某种方式将XElement.Value存储到模型中,就像您为Type所做的那样。像这样:

yourModelInstance.Type = (string)el.Attribute("type");
yourModelInstance.Value = (float)el;

您正在检索值的类型,而不是值本身。

您实际上可以将 XElement 转换为您期望的值的类型:

    var result = (float)gear.Attributes[0];

您可以在 https://msdn.microsoft.com/en-us/library/bb387049.aspx 阅读更多信息