如何在不创建冗余 class 的情况下将 XmlElement 的属性获取为双精度?
How do I get the attribute of an XmlElement as a double without creating a redundant class?
我有以下代码片段:
<XmlElement("point")> _
Public Property points() As List(Of Double)
Get
Return myPoints
End Get
Set(value As List(Of Double))
myPoints = value
End Set
End Property
参考我的 XML 文档的以下部分:
<upperLimit color="red">
<point y="12"/>
<point y="13"/>
<point y="14"/>
<point y="15"/>
<point y="16"/>
</upperLimit>
我试图告诉我的 VB 程序我希望它从我的 XML 文档中的 "points" 列表中创建一个双打列表。我无法理解的是我如何告诉它看 XmlElement
point
而不是 innerText
而是 XmlAttribute
y
所以像这样(我知道这是错误的)
<XmlElement("point").XmlAttribute("y")> _ <-- Notice this line!!
Public Property points() As List(Of Double)
Get
Return myPoints
End Get
Set(value As List(Of Double))
myPoints = value
End Set
End Property
我看到的唯一其他选择是创建另一个 class 来将值归因于。我什至想不出要在 google 上搜索什么来找到这个问题的答案...谢谢!
试试这个
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim xml As String = _
"<upperLimit color=""red"">" & _
"<point y=""12""/>" & _
"<point y=""13""/>" & _
"<point y=""14""/>" & _
"<point y=""15""/>" & _
"<point y=""16""/>" & _
"</upperLimit>"
Dim doc As XDocument = XDocument.Parse(xml)
Dim results As List(Of Double) = doc.Descendants("point").Select(Function(x) Double.Parse(x.Attribute("y").Value)).ToList()
End Sub
End Module
我有以下代码片段:
<XmlElement("point")> _
Public Property points() As List(Of Double)
Get
Return myPoints
End Get
Set(value As List(Of Double))
myPoints = value
End Set
End Property
参考我的 XML 文档的以下部分:
<upperLimit color="red">
<point y="12"/>
<point y="13"/>
<point y="14"/>
<point y="15"/>
<point y="16"/>
</upperLimit>
我试图告诉我的 VB 程序我希望它从我的 XML 文档中的 "points" 列表中创建一个双打列表。我无法理解的是我如何告诉它看 XmlElement
point
而不是 innerText
而是 XmlAttribute
y
所以像这样(我知道这是错误的)
<XmlElement("point").XmlAttribute("y")> _ <-- Notice this line!!
Public Property points() As List(Of Double)
Get
Return myPoints
End Get
Set(value As List(Of Double))
myPoints = value
End Set
End Property
我看到的唯一其他选择是创建另一个 class 来将值归因于。我什至想不出要在 google 上搜索什么来找到这个问题的答案...谢谢!
试试这个
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim xml As String = _
"<upperLimit color=""red"">" & _
"<point y=""12""/>" & _
"<point y=""13""/>" & _
"<point y=""14""/>" & _
"<point y=""15""/>" & _
"<point y=""16""/>" & _
"</upperLimit>"
Dim doc As XDocument = XDocument.Parse(xml)
Dim results As List(Of Double) = doc.Descendants("point").Select(Function(x) Double.Parse(x.Attribute("y").Value)).ToList()
End Sub
End Module