StealthRT
StealthRT

Reputation: 10542

XmlReader loop through node

Hey all i have the following code:

    Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
        reader.ReadToFollowing("GridChannel")
        Dim Channel As String = reader.GetAttribute("Channel")
        Dim DisplayName As String = reader.GetAttribute("DisplayName")

        reader.ReadToFollowing("Airings")
        reader.ReadToFollowing("GridAiring")

        Dim Title As String = reader.GetAttribute("Title")
        Dim EpisodeTitle As String = reader.GetAttribute("EpisodeTitle")
        Dim AiringTDurationime As String = reader.GetAttribute("AiringTDurationime")
        Dim isHD As Boolean = Convert.ToBoolean(reader.GetAttribute("isHD"))
        Dim TVRating As String = reader.GetAttribute("TVRating")
    End Using

That code works just fine above but i am having problems with looping through the Airings part.

The XML for that part looks like this:

    <Airings>
        <GridAiring ProgramId="35951" SeriesId="3490" Title="Matlock" EpisodeTitle="Santa Claus" AiringTime="2013-04-12T14:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="TV-PG" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="drama" Sports="false"/>
        <GridAiring ProgramId="828869" SeriesId="1409" Title="In the Heat of the Night" EpisodeTitle="Hatton's Turn" AiringTime="2013-04-12T15:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="TV-PG@V" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/>
        <GridAiring ProgramId="978338" SeriesId="1409" Title="In the Heat of the Night" EpisodeTitle="Hatton's Turn" AiringTime="2013-04-12T16:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="TV-PG@V" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/>
        <GridAiring ProgramId="4210626" Title="WGN Midday News" AiringTime="2013-04-12T17:00:00Z" Duration="60" Color="Color" AiringType="New" CC="true" LetterBox="false" Stereo="true" HD="false" SAP="false" TVRating="None" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="News" Subcategory="newscast" Sports="false"/>
        <GridAiring ProgramId="878716" SeriesId="1028666" Title="Walker, Texas Ranger" EpisodeTitle="El Coyote, Part 2" AiringTime="2013-04-12T18:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="true" HD="false" SAP="false" TVRating="TV-14@V" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/>
    </Airings>

Upvotes: 1

Views: 2278

Answers (3)

Lotok
Lotok

Reputation: 4607

Rather than looping through, you would get better performance with LINQ. Here is an example pulling the ProgramId, SeriesId and Title into an enumerable.

Dim document As XDocument = XDocument.Load("c:\tmp\test.xml")
Dim airings = From i In document.Descendants("Airings")
              Select New With
                     {Key .ProgramId = i.Attribute("ProgramId").Value,
                      Key .SeriesId = i.Attribute("SeriesId").Value,
                      Key .Title = i.Attribute("Title").Value}

You could then loop through the enumerable. A small example just printing the results in a console

For Each a In airings
    Console.WriteLine(String.Format("{0},{1},{2}", a.ProgramId, a.SeriesId, a.Title))
    Console.Read()
Next

The idea ofcourse being for each item you can just call .ProgramId, .SeriesId etc to get the value

If you wanted to add checking to be sure the Airing does not have a a missing attribute and prevent possible exceptions, you could add checks in the select to be sure it exists as below.

Dim document As XDocument = XDocument.Load("c:\tmp\test.xml")
Dim airings = From i In document.Descendants("Airings")
              Where i.Attribute("ProgramId") IsNot Nothing _
              And i.Attribute("SeriesId") IsNot Nothing _
              And i.Attribute("Title") IsNot Nothing
              Select New With
                     {Key .ProgramId = i.Attribute("ProgramId").Value,
                      Key .SeriesId = i.Attribute("SeriesId").Value,
                      Key .Title = i.Attribute("Title").Value}

Upvotes: 0

Victor Zakharov
Victor Zakharov

Reputation: 26424

Without going into XML serialization, why not use XmlReader.GetAttribute?

You should then be able to shrink your code down to this:

Dim ServiceId As String = reader.GetAttribute("ServiceId")

and so on, which is much more readable, and easier to maintain.

EDIT: to loop through an XML, I prefer this way:

Dim Airings As XDocument = XDocument.Parse(xmlString)
For Each GridAiring As XElement In Airings.Root.Elements
  Dim ProgramId As String = GridAiring.Attribute("ProgramId").Value
  'read other properties here
Next

Upvotes: 2

StealthRT
StealthRT

Reputation: 10542

Ok to loop you do this:

While reader.ReadToFollowing("GridAiring")
    Dim Title As String = reader.GetAttribute("Title")
    Dim EpisodeTitle As String = reader.GetAttribute("EpisodeTitle")
    Dim AiringTDurationime As String = reader.GetAttribute("AiringTDurationime")
    Dim isHD As Boolean = Convert.ToBoolean(reader.GetAttribute("isHD"))
    Dim TVRating As String = reader.GetAttribute("TVRating")
End While

Upvotes: 2

Related Questions