2016-03-24 3 views
1

Ich habe den folgenden Code-Snippet:Wie bekomme ich das Attribut eines XmlElement als Doppel, ohne eine redundante Klasse zu erstellen?

<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 

In Bezug auf den folgenden Abschnitt meines XML-Dokuments:

<upperLimit color="red"> 
       <point y="12"/> 
       <point y="13"/> 
       <point y="14"/> 
       <point y="15"/> 
       <point y="16"/> 
      </upperLimit> 

ich, dass ich es schaffen will mich versuche, mein VB-Programm zu sagen, Liste der Doubles aus der Liste der "Punkte" in meinem XML-Dokument. Was ich nicht verstehen kann, wie ich es sagen kann, an der XmlElementpoint schauen und nicht nehmen, es ist innerText sondern die XmlAttributey

So etwas wie dieses (ich weiß, das ist falsch)

<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 

Die einzige andere Alternative, die ich sehe, wäre, eine ANDERE Klasse zu erstellen, der der Wert zugewiesen werden kann. Ich kann auch nicht einmal daran denken, was ich bei Google suchen soll, um eine Antwort darauf zu finden ... Danke!

Antwort

0

Versuchen Sie, diese

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