问题描述
我一直在通读 LINQ 文档并查看 Stack Overflow 上的一些先前答案,但我仍然对 LINQ 的工作原理感到很困惑.我想从网站上获取一些数据,但我不知道如何让 xml 解析成字符串.这是我目前所拥有的:
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
因此,当表单加载时,它会将 XNamespace 设置为 ns,将 XDocument 设置为 xml.当我单击表单上的 cmdGetData 按钮时,它应该将网站名称加载到 XNamespace,然后获取第一个 id 元素的值并将其放入 strId 变量中.然后它应该在消息框中打印该值.我知道我做错了什么,但我不知道该怎么做才能解决它.
推荐答案
这是一个开始
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
问题描述
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.
推荐答案
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