Results 1 to 3 of 3

Thread: Help with CDATA

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Help with CDATA

    I have some data that I need to retrieve from an xml files CDATA as seen in <description> below.

    <Placemark>
    <name><![CDATA[Statistics]]></name>
    <description><![CDATA[Distance: 1.61 km (1.0 mi)
    Time: 7:56
    Moving Time: 7:53
    Average Speed: 12.18 km/h (7.6 mi/h)
    Average Moving Speed: 12.27 km/h (7.6 mi/h)
    Max Speed: 16.30 km/h (10.1 mi/h)
    Min Elevation: -24 m (-80 ft)
    Max Elevation: -10 m (-32 ft)
    Elevation Gain: 23 m (75 ft)
    Max Grade: 0 %
    Min Grade: 0 %
    ]]></description>
    <styleUrl>#sh_ylw-pushpin</styleUrl>
    <Point>
    <coordinates>-82.621966,27.773286</coordinates>
    </Point>
    </Placemark>
    <Placemark>

    I need to separate the data into individual strings like this:

    Time: 7:56
    Moving Time: 7:53
    Average Speed: 12.18 km/h (7.6 mi/h)
    Average Moving Speed: 12.27 km/h (7.6 mi/h)
    Max Speed: 16.30 km/h (10.1 mi/h)
    Min Elevation: -24 m (-80 ft)
    Max Elevation: -10 m (-32 ft)
    Elevation Gain: 23 m (75 ft)
    Max Grade: 0 %
    Min Grade: 0 %

    but when I load it into a textbox with the code below, it is all bunched together like this:

    Distance: 1.61 km (1.0 mi)Time: 7:56Moving Time: 7:53Average Speed:12.18 km/h (7.6 mi/h)Average Moving Speed: 12.27 km/h (7.6 mi/h)Max Speed:16.30 km/h (10.1 mi/h)Min Elevation: -24 m (-80 ft)Max Elevation: -10 m (-32 ft)Elevation Gain: 23 m (75 ft)Max Grade: 0 %Min Grade: 0 %

    Is there a way to separate the CDATA into separate lines as in the first example? The only thing I could think of was using split somehow?

    My Code

    DSet.ReadXmlSchema(strPath)
    DSet.ReadXml(strPath)
    DTable = DSet.Tables!Placemark
    Textbox.text = DTable.Rows(0).Item(1)

    Thanks

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Help with CDATA

    The following will extract your data. The function GetInfo was uses rather than a simple Split because several lines in the data has multiple colons which would cause incorrect results as the lines with say two colons would return too many elements

    Code:
    Private Sub CDataDemo()
        Dim query = (From T In XDocument.Load("YourDocumentNameGoesHere.xml")...<description> _
                     Select Item = T.Nodes.OfType(Of XCData).FirstOrDefault.Value).FirstOrDefault
    
        If query IsNot Nothing Then
    
            Dim Parts = (From x In (From T In query.Split(Chr(10)) _
                                    Where T.Length > 0 Select T) Let Item = GetInfo(x) _
                                    Select New With {.Description = Item(0), .Value = Item(1)}).ToList
    
            For Each line In Parts
                Console.WriteLine("->{0} -> {1}", line.Description, line.Value)
            Next
    
        End If
    End Sub
    Private Function GetInfo(ByVal sender As String) As String()
        Dim Pos = sender.IndexOf(":"c)
        If Pos > -1 Then
            Return New String() {sender.Substring(0, Pos), sender.Substring(Pos)}
        Else
            Return New String() {"", ""}
        End If
    End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Re: Help with CDATA

    Thanks! Worked out well for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width