0

I've been reading thorough the LINQ documentation and looking at some previous answers on Stack Overflow but I'm still pretty confused about how LINQ works. I want to grab some data from a website, but I can't figure out how to get the xml to parse into strings. Here is what I have so far:

Public Class Form1
    'Dim xml As XDocument
    Dim ns As XNamespace
    Dim strXMLSource As String = "http://gd2.mlb.com/components/game/mlb/year_2018/month_03/day_29/gid_2018_03_29_anamlb_oakmlb_1/linescore.xml"

    Dim xml As XDocument = <?xml version="1.0" encoding="utf-16"?>
                           <game>
                               <id>
                               </id>
                               <venue>
                               </venue>
                           </game>


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtXMLSource.Text = strXMLSource
    End Sub

    Private Sub cmdGetData_Click(sender As System.Object, e As System.EventArgs) Handles cmdGetData.Click
        ns = txtXMLSource.Text
        Dim strGame As XElement = xml.Descendants(ns + "game").First
        Dim strId As String = strGame.Descendants(ns + "id").First
        MessageBox.Show(strId)
    End Sub
End Class

So when the form loads it sets up an XNamespace as ns and an XDocument as xml. When I click the cmdGetData button on the form, it should load the website name to the XNamespace and then grab the value of the first id element and put it in the strId variable. And then it should print that value in a message box. I know I'm doing something wrong but I have no idea what to do to fix it.

5
  • As far as I can see, there is only one record there, so why do you need to query it? Commented Jan 12, 2018 at 23:01
  • Because there are a lot of similar pages, one for each mlb game during the season. Once I figure out how to make it work, I will build some logic to loop through the dates and build the urls and fetch the one record for each game. Commented Jan 13, 2018 at 0:47
  • The code above is basically based on @Neolisk 's answer here: stackoverflow.com/questions/21611098/… But now I'm thinking maybe it's not doing the web scraping part right? Commented Jan 13, 2018 at 1:56
  • It's totally unclear what you're doing in code. In cmdGetData_Click you effectively assign the values of strXMLSource to ns, i.e. you assign the web-address of XML to namespace variable. Total nonsense. Commented Jan 13, 2018 at 7:45
  • Yes I think I was confused about what the namespace is (still am). I just wanted to get the xml from a website and parse it. The second part of that seems to be well documented but the first part (getting from a web page) is not. All of the examples I have found online either use a file or type the xml right into the code, never from a website. Commented Jan 13, 2018 at 15:00

1 Answer 1

1

Here is a start

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const URL As String = "http://gd2.mlb.com/components/game/mlb/year_2018/month_03/day_29/gid_2018_03_29_anamlb_oakmlb_1/linescore.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(URL)
        Dim root As XElement = doc.Root

        Dim id As String = root.Attribute("id")
    End Sub

End Module
Sign up to request clarification or add additional context in comments.

2 Comments

Works great. Just a note - it works just as well without the 'Imports' statements. Is that because I'm running it from within Visual Studio? Ie, will I need to add the Imports statements if I release the code?
Different versions of VS start project with different default libraries. I automatically add the needed libraries just in case the user needs them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.