Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: A few questions about XML

  1. #21
    kingtam2000 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Posts
    25
    Reputation
    98
    Here's some code that fixes it along with some other edits.

    Code:
     
            Dim Fxml As New System.Xml.XmlDocument
            Fxml = New System.Xml.XmlDocument
            Fxml.Load("http://xoap.weather.com/weather/loca...8086eb4218acff")
            Dim Fnodes As System.Xml.XmlNodeList
            Dim Fnode As System.Xml.XmlNode
            Fnode = Fxml.SelectSingleNode("/weather/dayf") 'I moved this a level up so that you can access all the days, rather than just the first
            Fnodes = Fnode.ChildNodes
            Day1Label.Text = Fnode.SelectSingleNode("day").Attributes("dt").InnerText 'Instead of going through a for loop, just request the attribute directly
            Dim FNext As System.Xml.XmlNode
            Dim x As Integer = 1
            For Each FNext In Fnodes
                If FNext.Name = "day" Then
                    x = FNext.Attributes("d").InnerText '+ 1 if you want to x to be 1 when the day is today.
                    Fnode = FNext.SelectSingleNode("hi")
                    Select Case x 'Changed the if statments to a select
                        Case 1
                            Day1HighTemp.Text = Fnode.InnerText
                        Case 2
                            Day2HighTemp.Text = Fnode.InnerText
                        Case 3
                            Day3HighTemp.Text = Fnode.InnerText
                        Case 4
                            Day4HighTemp.Text = Fnode.InnerText
                    End Select
                    Fnode = FNext.SelectSingleNode("low")
                    Select Case x
                        Case 1
                            Day1LowTemp.Text = Fnode.InnerText
                        Case 2
                            Day2LowTemp.Text = Fnode.InnerText
                        Case 3
                            Day3LowTemp.Text = Fnode.InnerText
                        Case 4
                            Day4LowTemp.Text = Fnode.InnerText
                    End Select
                End If
            Next FNext
    As you can see I edited quite a lot, but I tried to comment most of the edits I made.

  2. #22
    Blake81's Avatar
    Blake81 is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Location
    Georgia, USA
    Posts
    304
    Reputation
    98
    Wow, thank you. That works. Actually, I didn't need to use your one comment about using +1 when the day is 0. It actually started on day 1 (which is what I wanted anyway), and I'm not really sure why, especially since you have this line:
    Code:
    x = FNext.Attributes("d").InnerText '+ 1 if you want to x to be 1 when the day is today.
    I'd think that would make it use day 0 since you set x equal to the value of that first attribute. I still need to fix the Day1Label line though. I left that there because I was just testing something. That's still showing the day 0 date, and I also have a label for each date, so I need to work that out. Thanks for your help. I really appreciate it, since I was really stuck on this one.

    I had another question posted, but I fixed it. Thanks again for your help.
    Last edited by Blake81; 02-26-2006 at 1:25 PM.

  3. #23
    Blake81's Avatar
    Blake81 is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Location
    Georgia, USA
    Posts
    304
    Reputation
    98
    I'm still not sure about working with attributes. I'm having a problem getting the right icon number for each day from the XML. Here's a small piece.
    [code]
    <dayd="1"t="Wednesday"dt="Feb 22">
    <
    hi>59</hi>
    <
    low>45</low>
    <
    sunr>7:17 AM</sunr>
    <
    suns>6:31 PM</suns>
    <
    partp="d">
    <icon>12</icon>
    [code]

    I need that icon value, and as you can see, it has to be for the right day, and I need to specify that "part p = d", because there are two part p sections in each day. Thanks.
    I tried this code, but it didn't work.
    Code:
    Fnode = Fxml.SelectSingleNode("/weather/dayf/day/part/icon")
    SelectCase x
    Case 1
    Day1Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 2
    Day2Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 3
    Day3Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 4
    Day4Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    EndSelect


    I've got half of it working. I can get to the icon, but it's starting at day 0. Here's what I have
    Code:
    SelectCase x
    Case 1
    Day1LowTemp.Text = Fnode.InnerText
    Case 2
    Day2LowTemp.Text = Fnode.InnerText
    Case 3
    Day3LowTemp.Text = Fnode.InnerText
    Case 4
    Day4LowTemp.Text = Fnode.InnerText
    EndSelect
    Dim DayIcon As System.Xml.XmlNode = Fxml.SelectSingleNode("/weather/dayf/day/part/icon")
    If y = "d"Then
    Day1Pic.Image = Image.FromFile("..\..\32\" + DayIcon.InnerText + ".jpg")
    EndIf

    Last edited by Blake81; 02-26-2006 at 5:11 PM.

  4. #24
    kingtam2000 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Posts
    25
    Reputation
    98
    The problem with your code is that you are not specifying which day or part. Using the code I posted previously, you could add this right after the select statements:
    Code:
     
    ForEach Fnode2 In FNext.SelectNodes("part")
    'Starting from the day node, select each node in the part node
    If Fnode2.Attributes("p").InnerText = "d"Then
    'Check if the attribute in the part node is "d"
    Select Case Fnode2.SelectSingleNode("icon").InnerText 
    'Get the icon value from the part node
    Case 1
    Day1Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 2
    Day2Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 3
    Day3Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    Case 4
    Day4Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")
    End Select
    EndIf
    Next
    
    You also have to create a variable Fnode2 before the for loop:
    Code:
    
    Dim Fnode2 As System.Xml.XmlNode
    
    Hope this helps

    BTW, the comment about the +1 was there in case you wanted to include today's temperature as well.

  5. #25
    Blake81's Avatar
    Blake81 is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Location
    Georgia, USA
    Posts
    304
    Reputation
    98
    In this line"
    Code:
    Day1Pic.Image = Image.FromFile("..\..\32\" + Fnode.InnerText + ".jpg")

    Should that be Fnode2? I ran it both ways, and it didn't put any icons in the boxes. Not sure if I did something wrong or not.

  6. #26
    kingtam2000 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Posts
    25
    Reputation
    98
    Very sorry, I pasted your code from the top and forgot to finish editing it, it should be changed to Fnode2.

  7. #27
    Blake81's Avatar
    Blake81 is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Location
    Georgia, USA
    Posts
    304
    Reputation
    98
    That code isn't working. I'm not getting any icons.

  8. #28
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,205
    Reputation
    2369
    You can also access an icon node directly by specifying which nodes/attributes to use in the xpath expression. (refer post 15)
    As for your question:
    Code:
    /weather/dayf/day/part/icon

    is the same as:
    Code:
    /weather/dayf/day[0]/part[0]/icon

    as you would want this for daytime day 1 (day 0 is current day):
    Code:
    /weather/dayf/day[@d='1']/part[@p='d']/icon

    and this for nighttime day 1:
    Code:
    /weather/dayf/day[@d='1']/part[@p='n']/icon

    since relativity is the key to xml, you rarely hardcode such a full path from root node, but take it from where you are when getting the different parts of data, for instance when iterating the day nodes you already got a reference to a 'daynode' and want the 'icon' childnode of its 'part' childnode where attribute 'p' is 'n' or 'd', so you just query something like this:
    Code:
    Dim IconString As String = daynode.SelectSingleNode("part[@p='d']/icon").InnerText
    http://www.w3schools.com/xpath/default.asp

  9. #29
    kingtam2000 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Posts
    25
    Reputation
    98
    My mistake, rushed the code a bit too much, I got confused with the select statment. Tell me if this code works, it should (I hope):
    Code:
     
    Select Case x
    'Get the day Here's where I went wrong
    Case 1
    Day1Pic.Image = Image.FromFile("..\..\32\" + Fnode2.SelectSingleNode("icon").InnerText + ".jpg")
    'Just continue the replacing it down here
    Alternatively, you could asign the Fnode2.SelectSingleNode("icon").InnerText value to a string to make the code slightly smaller.
    Code:
    dim iconnum as string
    ...
    For ...
    ...
    iconnum = Fnode2.SelectSingleNode("icon").InnerText
    Select Case x
    ...
    Day1Pic.Image = Image.FromFile("..\..\32\" + iconnum+ ".jpg")
    Didn't realise you could use xpath in that fashion to specify the attributes, so that would make coding easier, if you only wanted the day icon, instead of both.

  10. #30
    Blake81's Avatar
    Blake81 is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Location
    Georgia, USA
    Posts
    304
    Reputation
    98
    Thanks for your help. I got it working now. I have a tool strip drop down menu for each zip code they want to add to their favorites and I'm trying to add the functionality to a "delete favorite" button. I've been looking into it, and There's a ToolStripMenuItem.Add, but there isn't a remove. Any ideas on how I'd do that? I am also saving their favorite zip codes in My.Settings as they add them, and I'm not totally sure that my method for deleting from there will work either. It says you have to reference the items on the list by number, as if they're in some kind of array, but I don't know how to do that, since I don't know what zip codes they'll have and in what order. Here's the code for something I tried. Not sure if this will do it.
    Code:
    PrivateSub DeleteFavLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteFavLocation.Click
    If ZIP.Text <> NothingThen
    IfMy.Settings.FavoriteZipOne = ZIP.Text Then
    Dim DelFindOne As Array = ChooseFavLocation.DropDownItems.Find(My.Settings.FavoriteZipOne, 1)
    My.Settings.FavoriteZipOne = ""
    ChooseFavLocation.DropDownItems.Remove(DelFindOne(1))
    ElseIfMy.Settings.FavoriteZipTwo = ZIP.Text Then
    Dim DelFindTwo As Array = ChooseFavLocation.DropDownItems.Find(My.Settings.FavoriteZipTwo, 1)
    My.Settings.FavoriteZipTwo = ""
    ChooseFavLocation.DropDownItems.Remove(DelFindTwo(1))
    ElseIfMy.Settings.FavoriteZipThree = ZIP.Text Then
    Dim DelFindThree As Array = ChooseFavLocation.DropDownItems.Find(My.Settings.FavoriteZipThree, 1)
    My.Settings.FavoriteZipThree = ""
    ChooseFavLocation.DropDownItems.Remove(DelFindThree(1))
    EndIf
    EndIf
    EndSub


Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking