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

How to read a number inside an attribute type

谁能告诉我怎么读这个 XML 中的数字 20.0?我在 XML.LinQ 中使用 c# 和 XElements。

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

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

gear.Attributes[0].Type.Value;

我明白了 "float"

我这样定义 "Type"

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

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

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

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

您可以在 https://msdn.microsoft.com/en-us/library/bb387049.aspx

阅读更多内容

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

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