[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

XML Read question...

Hello

I am new to xml and I have a question:

I have this XML-File:

<?xml version="1.0" encoding="us-ascii"?>
<!-- DVD Profiler Version 2.3.1 Collection Export -->
<!-- Exported: 13.04.2005 19:26:06 -->
<Collection>
<DVD>
  <Title>Austin Powers: Spion in geheimer Missionarsstellung</Title>
  <SortTitle>Austin Powers 2</SortTitle>
  <Regions>
    <Region>2</Region>
  </Regions>
  <CollectionType>Owned</CollectionType>
  <CollectionNumber>408</CollectionNumber>
  <ProductionYear>1999</ProductionYear>
  <Released>2000-08-08</Released>
  <RunningTime>91</RunningTime>
  <Genres>
    <Genre>Comedy</Genre>
  </Genres>
  <Format>
    <FormatAspectRatio>2.35</FormatAspectRatio>
    <FormatVideoStandard>PAL</FormatVideoStandard>
    <FormatLetterBox>True</FormatLetterBox>
    <FormatPanAndScan>False</FormatPanAndScan>
    <FormatFullFrame>False</FormatFullFrame>
    <Format16X9>True</Format16X9>
    <FormatDualSided>False</FormatDualSided>
    <FormatDualLayered>True</FormatDualLayered>
    <FormatFlipper>False</FormatFlipper>
  </Format>
 </DVD>
<DVD>
  <Title>Austin Powers332</Title>
  <SortTitle>Austin Powers 3</SortTitle>
  <Regions>
    <Region>5</Region>
  </Regions>
  <CollectionType>Owned</CollectionType>
  <CollectionNumber>401</CollectionNumber>
  <ProductionYear>1992</ProductionYear>
  <Released>2001-08-08</Released>
  <RunningTime>45</RunningTime>
  <Genres>
    <Genre>Comedy</Genre>
  </Genres>
  <Format>
    <FormatAspectRatio>2.35</FormatAspectRatio>
    <FormatVideoStandard>PAL</FormatVideoStandard>
    <FormatLetterBox>True</FormatLetterBox>
    <FormatPanAndScan>False</FormatPanAndScan>
    <FormatFullFrame>False</FormatFullFrame>
    <Format16X9>True</Format16X9>
    <FormatDualSided>False</FormatDualSided>
    <FormatDualLayered>True</FormatDualLayered>
    <FormatFlipper>False</FormatFlipper>
  </Format>
 </DVD>
</Collection>


How can I now read all <Title> into a listbox and - if selected -showing  the <Released>-Value of the selected DVD ?

Thanks

Sven
0
Ruttensoft
Asked:
Ruttensoft
  • 9
  • 6
  • 2
1 Solution
 
YZlatCommented:
read XML data into DataSet and then Bind your ListBox to the DataSet
0
 
YZlatCommented:
Dim myXMLfile As String = "C:\MyFile.xml"
            Dim ds As New DataSet

    Dim fs As New System.IO.FileStream(myXMLfile, System.IO.FileMode.Open)
            Try
                ds.ReadXml(fs)
                ListBox1.DataSource = ds
                ListBox1.DisplayMember = "Title"
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            Finally
                fs.Close()
            End Try
0
 
S-TwilleyCommented:
well, there's probably a much better way than this, but:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myDoc As New Xml.XmlDocument
        myDoc.Load("D:\My Documents\My Webs\My.xml")    ' replace this with the filename where your xml data is, or a stream, or change it for a string with xml code in

        Dim xnCollection As XmlNode = myDoc.SelectSingleNode("Collection")
        Dim xnDVD, xnTitle As XmlNode

        For Each xnDVD In xnCollection.ChildNodes
            If xnDVD.Name.ToLower = "dvd" Then
                Try
                    xnTitle = xnDVD.SelectSingleNode("Title")
                    ListBox1.Items.Add(xnTitle.InnerText)
                Catch ex As Exception

                End Try
            End If
        Next
    End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
S-TwilleyCommented:
There you go, there was a neater way after all  !

I'm still not used to using datasets myself :P... i kinda like to control my own destiny/data so im always reluctant to use them
0
 
RuttensoftAuthor Commented:
Thanks - And how can I solve the second question?

If I now press on a listbox-entry I want to display the CollectionNumber of the selected DVD:..


Thanks

Sven
0
 
S-TwilleyCommented:
Well... im not sure about the dataset way :\     but  if you want to attach other details with that Title in the listbox, then it's easier to make a class to hold the data

Public Class VideoItem
    Public Title As String
    Public CollectionNumber As String

    Public Overrides Function ToString() As String
        Return Title
    End Function
End Class

=================================

in your form:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myDoc As New Xml.XmlDocument
        myDoc.Load("D:\My Documents\My Webs\My.xml")    ' replace this with the filename where your xml data is, or a stream, or change it for a string with xml code in

        Dim xnCollection As XmlNode = myDoc.SelectSingleNode("Collection")
        Dim xnDVD, xnTitle, xnNumber As XmlNode

        For Each xnDVD In xnCollection.ChildNodes
            If xnDVD.Name.ToLower = "dvd" Then
                Try
                    xnTitle = xnDVD.SelectSingleNode("Title")
                    xnNumber = xnDVD.SelectSingleNode("Title")
                    Dim thisItem as new VideoItem()
                    thisItem.Title  = xnTitle
                    thisItem.CollectionNumber= xnNumber

                    ListBox1.Items.Add(thisItem)
                Catch ex As Exception

                End Try
            End If
        Next
    End Sub

==========================

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedItem Is Nothing Then
            'do nothing
        Else
            Dim thisItem As VideoItem = DirectCast(ListBox1.SelectedItem, VideoItem)
            MsgBox(thisItem.CollectionNumber)
        End If
    End Sub


=======================

It's by no means a neat class, and you could make it more secure, but that should show you one way of doing it
0
 
RuttensoftAuthor Commented:
Thanks but what is the VideoItem? It gives me an "VideoItem is undeclared"-Error...
0
 
S-TwilleyCommented:
VideoItem is a class... it's at the top of my last post
0
 
S-TwilleyCommented:
you need to put it in with your code... you could add a new class and paste it in, or put it at the end of your form code... up to you really
0
 
RuttensoftAuthor Commented:
I have increased the points to 500 for another 2 questions:

1. As you can see, there is a <Genres> with <Genre>-Subitem. This one can have multiple entrys...
How can I read all the <Genre> and make a string: genre1|genre2|genre3 etc?

2. And how can I read all subitems of <Format> which are now different, not like the genre where every item was genre...

Thanks

Sven
0
 
S-TwilleyCommented:
what do you want done when these are read?  do you want them stored within the class as well i.e. linked in with each listbox entry?
0
 
S-TwilleyCommented:
btw... in my previous example... the lines should have read

                    thisItem.Title = xnTitle.InnerText
                    thisItem.CollectionNumber = xnNumber.InnerText
0
 
RuttensoftAuthor Commented:
Yes exactly, linked with each listbox entry!
0
 
RuttensoftAuthor Commented:
Thanks I have had already corrected this...
0
 
S-TwilleyCommented:
updated class:

=====================

Public Class VideoItem
    Public Title As String
    Public CollectionNumber As String
    Public Genres As New ArrayList
    Public FormatData As New Hashtable

    Public Overrides Function ToString() As String
        Return Title
    End Function

    Public ReadOnly Property GenreList() As String
        Get
            If Genres Is Nothing Then
                Return "No Entries"
            Else
                If Genres.Count = 0 Then
                    Return "No Entries"
                ElseIf Genres.Count = 1 Then
                    Return Genres.Item(0)
                Else
                    Return String.Join(", ", Genres.ToArray)
                End If
            End If
        End Get
    End Property

    Public ReadOnly Property FormatList() As String
        Get
            Dim thisEntry As DictionaryEntry
            Dim rStr As String = ""
            For Each thisEntry In FormatData
                rStr &= thisEntry.Key & ": " & thisEntry.Value & " <> "
            Next

            If rStr.EndsWith(" <> ") Then
                rStr = rStr.Substring(0, rStr.Length - 4)
            End If

            If rStr.Length > 0 Then
                Return rStr
            Else
                Return "No Entries"
            End If
        End Get
    End Property

End Class

=====================




FORM CODE:

updated button for loading document, and adding to list:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myDoc As New Xml.XmlDocument
        myDoc.Load("D:\My Documents\My Webs\My.xml")    ' replace this with the filename where your xml data is, or a stream, or change it for a string with xml code in

        Dim xnCollection As XmlNode = myDoc.SelectSingleNode("Collection")
        Dim xnDVD, xnTitle, xnNumber, xnGenres, xnFormats, xnGenre, xnFormat As XmlNode

        For Each xnDVD In xnCollection.ChildNodes
            If xnDVD.Name.ToLower = "dvd" Then
                Try
                    xnTitle = xnDVD.SelectSingleNode("Title")
                    xnNumber = xnDVD.SelectSingleNode("Title")
                    xnGenres = xnDVD.SelectSingleNode("Genres")
                    xnFormats = xnDVD.SelectSingleNode("Format")



                    Dim thisItem As New VideoItem
                    thisItem.Title = xnTitle.InnerText
                    thisItem.CollectionNumber = xnNumber.InnerText

                    For Each xnGenre In xnGenres.ChildNodes
                        thisItem.Genres.Add(xnGenre.InnerText)
                    Next

                    For Each xnFormat In xnFormats.ChildNodes
                        thisItem.FormatData.Add(xnFormat.Name, xnFormat.InnerText)
                    Next

                    ListBox1.Items.Add(thisItem)
                Catch ex As Exception

                End Try
            End If
        Next
    End Sub


===========

item select event:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedItem Is Nothing Then
            'do nothing
        Else
            Dim thisItem As VideoItem = DirectCast(ListBox1.SelectedItem, VideoItem)
            MsgBox(thisItem.CollectionNumber)
            MsgBox(thisItem.FormatList)
            MsgBox(thisItem.GenreList)
        End If
    End Sub



0
 
S-TwilleyCommented:
you can see in the FormatList property of the class, i have put if in the format

FormatDetail:  FormatValue   <>  formatDetail2: formatvalue2

you can change the way it's displayed quite easily, i think it's simple enough to see


======

same goes for genres, when there is more than one item i nthe list, it is seperated by commas, which can easily be changed
0
 
RuttensoftAuthor Commented:
Thanks alot

Sven
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now