XMLTextReader - XML node name problem, need nested name and parent

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-size:small Hello,
<span style="font-size:small Im fairly new to coding in VB, so please be understanding!
Im creating an application that reads XML data and stores it in list boxes.
The main problem Im having is that I cant get the proper <title> from each broadcast. My output is getting BBC ALBA which is a <title> node above the one I need.
I think I need to be more specific with my node name, and find the <title> nested inside <display_titles>.
For example, I should be getting this for the first broadcast ListBox output:
00:00
Seo Alba (This is Alba)
11/12/2012
Sealladh ùr bho Alba. A new view from Scotland.
But Im getting everything right, except BBC ALBA instead of Seo Alba (This is Alba)
Heres my code:
<pre class="prettyprint lang-vb Sub Load_Schedule_XML()

*** XmlTextReader
Dim XmlReader As New XmlTextReader("http://www.bbc.co.uk/bbcalba/programmes/schedules/2012/12/11.xml")

Dim intID, intBroadcastCountTotal As String
Dim strStart, strSynopsis, strTitle, strSubtitle As String

ScheduleListBox.Items.Clear()

While XmlReader.Read()

extract the atribute "pid" value when the reader reach the "broadcast" element.
If XmlReader.Name.ToString() = "broadcast" Then
If XmlReader.HasAttributes Then
While XmlReader.MoveToNextAttribute()
If XmlReader.Name = "pid" Then
intID = XmlReader.Value
End If
End While
End If
End If
If XmlReader.Name.ToString() = "start" Then strStart = Trim(XmlReader.ReadString())
If XmlReader.Name.ToString() = "short_synopsis" Then strSynopsis = Trim(XmlReader.ReadString()
If XmlReader.Name.ToString() = "title" Then strTitle = Trim(XmlReader.ReadString())
If XmlReader.Name.ToString() = "subtitle" Then strSubtitle = Trim(XmlReader.ReadString())

When the end of each <broadcast> element is reached by the XmlReader, you can extract all the values you need and import them into a database or whatever
These readers provide forward-only access to XML data, so I choose to extract the data from each of the elements, and then use the values
If XmlReader.Name.ToString() = "broadcast" And XmlReader.NodeType.ToString() = "EndElement" Then

ScheduleListBox.Items.Add(strStart.Substring(11, 5))
ScheduleListBox.Items.Add(strTitle)
ScheduleListBox.Items.Add(strSubtitle)
ScheduleListBox.Items.Add(strSynopsis)
ScheduleListBox.Items.Add("")

Reset the values for next broadcast
intID = 0
strStart = ""
strTitle = ""
strSubtitle = ""
strSynopsis = ""

intBroadcastCountTotal += 1

End If

End While

XmlReader.Close()

Schedule_Broadcast_Total_Label.Text = "Total Broadcasts = " & CStr(intBroadcastCountTotal)

End Sub[/code]
<br/>
Heres my XML:
<pre class="prettyprint <?xml version="1.0"?>
-<schedule>-<service key="bbcalba" type="tv <title>BBC ALBA</title></service>-<day has_previous="1" has_next="1" date="2012-12-11 -<broadcasts>-<broadcast is_blanked="0" is_repeat="0 <pid>p01228ny</pid><start>2012-12-11T00:00:00Z</start><end>2012-12-11T06:00:00Z</end><duration>21600</duration>+<programme type="episode ------</broadcast>-<broadcast is_blanked="0" is_repeat="0 <pid>p0123fxg</pid><start>2012-12-11T06:00:00Z</start><end>2012-12-11T16:58:00Z</end><duration>39480</duration>-<programme type="episode <pid>b01p96n9</pid><position/><title>11/12/2012</title><short_synopsis>Sealladh ùr bho Alba. A new view from Scotland.</short_synopsis><media_type>audio_video</media_type><duration>39480</duration>-<display_titles><title>Seo Alba (This is Alba)</title><subtitle>11/12/2012</subtitle></display_titles><first_broadcast_date>2012-12-11T06:00:00Z</first_broadcast_date>-<ownership>-<service key="bbcalba" type="tv" id="bbc_alba <title>BBC ALBA</title></service></ownership>-<programme type="brand <pid>b01k2h0w</pid><title>Seo Alba (This is Alba)</title><position/><expected_child_count/><first_broadcast_date>2011-07-03T06:00:00+01:00</first_broadcast_date>-<ownership>-<service key="bbcalba" type="tv" id="bbc_alba <title>BBC ALBA</title></service></ownership></programme><is_available_mediaset_pc_sd>0</is_available_mediaset_pc_sd><is_legacy_media>0</is_legacy_media></programme></broadcast>+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 ----+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 ---------+<broadcast is_blanked="0" is_repeat="0 -------+<broadcast is_blanked="0" is_repeat="0 --------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="0 --------+<broadcast is_blanked="0" is_repeat="1 --------+<broadcast is_blanked="0" is_repeat="1 ---------+<broadcast is_blanked="0" is_repeat="1 -------+<broadcast is_blanked="0" is_repeat="0 -------</broadcasts></day></schedule>[/code]
<br/>

View the full article
 
Back
Top