8 Replies - 630 Views - Last Post: 17 October 2011 - 07:25 PM Rate Topic: -----

#1 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 15 October 2011 - 06:56 PM

Hello all, I'm in need of some help. I just started learning VB.NET about a moth ago (I have been taking classes, so its been about 5 days worth of classes) and I decided to work on this project idea I had. Essentially, it's a database to keep track of anime and manga that I, or whoever else uses it, have watched/read. So far I was able to populate a listbox with the following code:
    Dim doc As New Xml.XmlDocument
        doc.Load("XML.xml")
        For Each node As Xml.XmlNode In doc.SelectNodes("anime/series/title")
            titleList.Items.Add(node.InnerText)
        Next

An example of the XML code:
<anime>
  <series>
    <title>Hidamari Sketch</title>
    <season>01</season>
    <episodes>12</episodes>
    <studio>SHAFT</studio>
    <trans>Spoon Subs</trans>
    <directory>O:\Videos\Anime\Hidamari Sketch\</directory>
  </series>
</anime>



However, now I want to make it so that when one selects a specific title in the list box, it will show the data in the tags below the <series> tag that matches the item selected in a label. One thing I just thought of is than since the list box gets populated in the order that the series are in, it could be easier to go in order from first item equaling the first <series> and so on. The only problem is I have no idea how to do this. Does anyone have any reference material or some samples that could give me an idea of what to do?

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Data Below Specific XML Tag By Selecting from Listbox

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 15 October 2011 - 07:41 PM

The structure of the xml is perfect for a dataset. The dataset has a ReadXml method for reading an xml file and making it databindable. Add this to the top of the file: <?xml version="1.0" standalone="yes"?>


Private ds As New DataSet

'form load event
Dim filepath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "xml.xml")
If IO.File.Exists(filepath) Then
   ds.ReadXml(filepath)
   dgv.DataSource = ds.Tables(0)
End If


I used a datagridview (dgv variable) to show all the data, but you can add databindings to many controls now. If this sounds like an idea let me know and I'll help you set it up.

This post has been edited by _HAWK_: 15 October 2011 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 15 October 2011 - 08:58 PM

The other thing you can do is get the Series element and then parse it's child node.

Dim series As XmlNode = GetSeriesElement(titleList.Text)
If Not series Is Nothing Then
  labelTitle.Text = series.ChildNode(0).InnerText
  labelSeason.Text = series.ChildNode(1).InnerText
  'etc...
  ' you have 6 child nodes so 0 - 5 
End If



Private Function GetSeriesElement(title As String) As XmlNode
  Dim seriesElements As XmlNodeList = doc.SelectNodes("anime/series")
  If Not seriesElements Is Nothing Then
     For Each series As XmlNode In seriesElements
         If series.InnerText.Equals(title) Then Return series
     Next
  End If
  Return Nothing
End Function

Was This Post Helpful? 1
  • +
  • -

#4 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 01:14 PM

Sorry for the late response, I was busy all day yesterday. Thank you for replying. So _HAWK_, where would I put that first block of code in your second reply? would it just go under "Private Sub titleList" (the list box) or under "Private Sub dataLabel" (the label box)?
Was This Post Helpful? 0
  • +
  • -

#5 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 01:41 PM

Nevermind the last post, I get it now.
Was This Post Helpful? 0
  • +
  • -

#6 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 02:07 PM

labelTitle.Text = series.ChildNode(0).InnerText


Wouldn't it be "series.ChildNodes"?
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 04:06 PM

Yep, thats a typo.

This post has been edited by _HAWK_: 17 October 2011 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 04:37 PM

Thanks again. Going back to my first question that I guess I was wrong about, Where would the first block of code go (where all the labels are being filled)? Also, the "End Function" is giving me an error ("Function 'GetSeriesElement' doesn't return a value on all code paths...."). I'm not to sure why this is (we haven't gotten that far in class and I cant find a solution on google).
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Displaying Data Below Specific XML Tag By Selecting from Listbox

Posted 17 October 2011 - 07:25 PM

I would put it in the Selected_Index_Changed event for the listbox and surround it with If Not titleList.SelectedIndex = -1 Then.... The warning your getting - I don't get. It means, is there a place where the code could miss hitting the Return statement thus not returning a value - hence the design of a function. In my example if the For Loop did not find it the Return at the bottom would execute. With this pattern you should set a variable to the function and then test that it does not = Nothing - you know the return value of a missing name.

This post has been edited by _HAWK_: 17 October 2011 - 07:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1