EDN Admin
Well-known member
Hello, I am having issues using the xmlreader. I have more explanation of the issue after the code below.
here is an example of my xml:
<CATALOG>
<DVD>
<TITLE>title one</TITLE>
<FRONT_COVER>picture1-1.jpg</FRONT_COVER>
<BACK_COVER>picture1-2.jpg</BACK_COVER>
<ACTORS>
<ACTOR>actor1-1</ACTOR>
<ACTOR>actor1-2</ACTOR>
</ACTORS>
</DVD>
<DVD>
<TITLE>title two</TITLE>
<FRONT_COVER>picture2-1.jpg</FRONT_COVER>
<BACK_COVER>picture2-1.jpg</BACK_COVER>
<ACTORS>
<ACTOR>actor2-1</ACTOR>
<ACTOR>actor2-2</ACTOR>
</ACTORS>
</DVD>
</CATALOG >
and here is my code, take from murachs vb2008 pg 699
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dvds As New List(Of objDVD)
Dim createTable As String
createTable = ""
create xmlReaderSettings object
Dim settings As New XmlReaderSettings
settings.IgnoreWhitespace = True
settings.IgnoreComments = True
create xmlReader object
Dim xmlIn As XmlReader = XmlReader.Create(Path, settings)
read past all nodes to the first dvd node
If xmlIn.ReadToDescendant("DVD") Then
create one DVD object for each DVD node
Do
Dim dvd As New objDVD
xmlIn.ReadStartElement("DVD")
dvd.title = xmlIn.ReadElementContentAsString
dvd.fCoverPath = xmlIn.ReadElementContentAsString
dvd.bCoverPath = xmlIn.ReadElementContentAsString
dvds.Add(dvd)
Loop While xmlIn.ReadToNextSibling("DVD")
End If
xmlIn.Close()
For Each dvd In dvds
createTable += "<tr><td> & Chr(34) & vidPath & dvd.title & Chr(34) & " & dvd.title & " </td><td><img src=" & _
Chr(34) & imgPath & dvd.fCoverPath & Chr(34) & " height=" & Chr(34) & "150" & Chr(34) & "width=" & Chr(34) & "100" & _
Chr(34) & "/>" & "</td><td><img src=" & Chr(34) & imgPath & dvd.bCoverPath & Chr(34) & " height=" & Chr(34) & "150" _
& Chr(34) & "width=" & Chr(34) & "100" & Chr(34) & "/>" & "</td></tr>"
Next
End Sub
End Class
When I spin though the dvds list, I only have an index of 1, and I would expect both title nodes would of been pulled in to the list through the do while statement. I cant figure out what is wrong, bc it seems like it should work. Any help would be appreciated.
Thanks, Jason
View the full article
here is an example of my xml:
<CATALOG>
<DVD>
<TITLE>title one</TITLE>
<FRONT_COVER>picture1-1.jpg</FRONT_COVER>
<BACK_COVER>picture1-2.jpg</BACK_COVER>
<ACTORS>
<ACTOR>actor1-1</ACTOR>
<ACTOR>actor1-2</ACTOR>
</ACTORS>
</DVD>
<DVD>
<TITLE>title two</TITLE>
<FRONT_COVER>picture2-1.jpg</FRONT_COVER>
<BACK_COVER>picture2-1.jpg</BACK_COVER>
<ACTORS>
<ACTOR>actor2-1</ACTOR>
<ACTOR>actor2-2</ACTOR>
</ACTORS>
</DVD>
</CATALOG >
and here is my code, take from murachs vb2008 pg 699
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dvds As New List(Of objDVD)
Dim createTable As String
createTable = ""
create xmlReaderSettings object
Dim settings As New XmlReaderSettings
settings.IgnoreWhitespace = True
settings.IgnoreComments = True
create xmlReader object
Dim xmlIn As XmlReader = XmlReader.Create(Path, settings)
read past all nodes to the first dvd node
If xmlIn.ReadToDescendant("DVD") Then
create one DVD object for each DVD node
Do
Dim dvd As New objDVD
xmlIn.ReadStartElement("DVD")
dvd.title = xmlIn.ReadElementContentAsString
dvd.fCoverPath = xmlIn.ReadElementContentAsString
dvd.bCoverPath = xmlIn.ReadElementContentAsString
dvds.Add(dvd)
Loop While xmlIn.ReadToNextSibling("DVD")
End If
xmlIn.Close()
For Each dvd In dvds
createTable += "<tr><td> & Chr(34) & vidPath & dvd.title & Chr(34) & " & dvd.title & " </td><td><img src=" & _
Chr(34) & imgPath & dvd.fCoverPath & Chr(34) & " height=" & Chr(34) & "150" & Chr(34) & "width=" & Chr(34) & "100" & _
Chr(34) & "/>" & "</td><td><img src=" & Chr(34) & imgPath & dvd.bCoverPath & Chr(34) & " height=" & Chr(34) & "150" _
& Chr(34) & "width=" & Chr(34) & "100" & Chr(34) & "/>" & "</td></tr>"
Next
End Sub
End Class
When I spin though the dvds list, I only have an index of 1, and I would expect both title nodes would of been pulled in to the list through the do while statement. I cant figure out what is wrong, bc it seems like it should work. Any help would be appreciated.
Thanks, Jason
View the full article