在VB中解析XML项目 [英] Parsing XML items in VB
问题描述
我想从我指定的xml feed链接中获取每个项目。这是XML格式,
I want to get each items from xml feed link i specify. Here is the XML format,
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>
Site title
</title>
<link>http://www.blahblah.com</link>
<language>ru</language>
<description>
This site rocks
</description>
<generator>DataLife Engine</generator>
<item>
<title>
Item no 1 title
</title>
<guid isPermaLink="true">
http://www.blahblah.com/item1
</guid>
<link>
http://www.blahblah.com/item1
</link>
<description>
Description of item 1
</description>
<category>
Category 0
</category>
<dc:creator>admin</dc:creator>
<pubDate>Fri, 19 Sep 2014 08:00:00 +0000</pubDate>
</item>
<item>
<title>
Item no 2 title
</title>
<guid isPermaLink="true">
http://www.blahblah.com/item2
</guid>
<link>
http://www.blahblah.com/item2
</link>
<description>
Description of item 2
</description>
<category>
Category 0
</category>
<dc:creator>admin</dc:creator>
<pubDate>Fri, 19 Sep 2014 07:00:00 +0000</pubDate>
</item>
</channel>
</rss>
这是Feed中的示例项。对于每个
Here is a sample item in a feed. For each
<item></item>
那里,它有自己的标题,描述和页面链接。
我希望保留标题在TextBox 1中,在TextBox 2中链接,在TextBox 3中仅为第一项描述。
大多数情况下,我希望将它们保存为字符串以便我可以继续我的代码使用这些字符串。
任何人都可以帮我这个吗?
编辑:
there, it has its own title, description and link to the page.
I want the title to be kept in TextBox 1, link in TextBox 2, and decription in TextBox 3 for the first item only.
Mostly, I want them to be saved as a string so I can continue with my code using those strings.
Can anyone help me with this ?
Edit :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
File.Delete(paths)
If Not File.Exists(paths) Then
File.Create(paths).Dispose()
End If
' Dim Lines() As String
Dim stringSeparators() As String = {vbCrLf}
Dim Source As String
Dim wc As New WebClient
Source = wc.DownloadString("http://www.mrgoogleglass.com/feed")
File.WriteAllText(paths, Source)
Dim strreader As New StreamReader(paths, System.Text.Encoding.UTF8)
Dim xmlreader As XmlTextReader = New XmlTextReader(strreader)
xmlreader.WhitespaceHandling = WhitespaceHandling.None
Dim xmldoc As New XmlDocument()
xmldoc.Load(xmlreader)
Dim xml_nodelist As XmlNodeList = xmldoc.GetElementsByTagName("/item")
Dim mStatus As String
Dim mID As Integer
Dim mCountry As String
Dim mCourse As String
Dim mDate As Integer
Dim mDrawAdvantage As String
Dim mAdvancedGoing As String
For Each mxmlnode As XmlElement In xmldoc.GetElementById("item")
mStatus = mxmlnode.Attributes.ItemOf("title").InnerXml
'mID = mxmlnode.Attributes.ItemOf("id").InnerText
'mCountry = mxmlnode.Attributes.ItemOf("country").InnerText
'mCourse = mxmlnode.Attributes.ItemOf("course").InnerText
'mDate = mxmlnode.Attributes.ItemOf("date").InnerText
'mDrawAdvantage = mxmlnode.SelectSingleNode("DrawAdvantage").InnerText
'mAdvancedGoing = mxmlnode.SelectSingleNode("AdvancedGoing").InnerText
MsgBox(mStatus)
Next
这样,我已经设法下载整个文件并将其保存为字符串。但我真的无法完成最后一部分。我从以下代码获得了代码,在vb .NET中读取和解析XML [ ^ ]
但这个问题集中在元素的属性上。我实际上想要这些元素。
this way, i have managed to download the whole file and save it as string. But i am not really able to get done with the last part. I got the code from, Reading and Parsing XML in vb .NET[^]
but that question focused on the attributes of the elements. I want the elements actually.
推荐答案
您只需要阅读和理解有关该主题的原始MSDN文档即可。 .NET FCL提供了不同的XML解析方法。这是我对它们的快速概述:
All you need it to read and understand original MSDN documentation on the topic which is readily available. There are different approaches to XML parsing offered by .NET FCL. This is my quick overview of them:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。 - 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。 - 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. - Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^]. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
我找到了 2种不同的解决方案。
解决方案1:
I have found 2 different solutions.
Solution 1 :
Imports System.IO
Imports System.Xml
Imports System.Net
Public Class Form1
WithEvents bs As New BindingSource
Dim paths As String = Application.StartupPath + "/eeeror.log"
Dim source As String = File.ReadAllText(paths)
Dim paths1 As String = Application.StartupPath + "/123.log"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
File.Delete(paths1)
If Not File.Exists(paths1) Then
File.Create(paths1).Dispose()
End If
Dim ds As New DataSet
Dim sr As System.IO.StringReader = New System.IO.StringReader(source)
ds.ReadXml(sr, XmlReadMode.InferSchema)
bs.DataSource =
(
From T In ds.Tables("item")
Select New Item With
{
.Description = T.Field(Of String)("description").Trim,
.Link = T.Field(Of String)("link").Trim,
.Title = T.Field(Of String)("title").Trim
}
).ToList
For Each i As Item In bs
Next
ds = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim intITEMS As Integer = -1 'starting at -1 as array can work with (0)
For Each itemint As Item In bs
intITEMS += 1
MsgBox(intITEMS.ToString)
Dim item As Item = CType(bs.Item(intITEMS), Item)
File.AppendAllText(paths1, item.Title + ":" + item.Link + ":" + item.Description + vbCrLf)
Next
End Sub
End Class
Public Class Item
Public Property Title As String
Public Property Link As String
Public Property Description As String
Public Sub New()
End Sub
Public Overrides Function ToString() As String
Return String.Concat(Title, ", ", Description, ", ", Link)
End Function
End Class
这样,你得到123.log文件中的行数= rss中的项目。没有错误。
解决方案2:
This way, you get the number of lines in the 123.log file = items in rss. There will be no errors.
Solution 2 :
Imports System.IO
Imports System.Xml
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
Dim paths As String = Application.StartupPath + "/eeeror.log"
Dim source As String = File.ReadAllText(paths)
Dim paths1 As String = Application.StartupPath + "/123.log"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
File.Delete(paths1)
If Not File.Exists(paths1) Then
File.Create(paths1).Dispose()
End If
Dim xmlString As String = My.Computer.FileSystem.ReadAllText(paths)
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
Dim itemcount As Integer = Regex.Matches(source, "<item>").Count
For i As Integer = 1 To itemcount + 1 '+1 because number <items> tag + with the title as it was included, this prevents craching
Dim title As String = ""
Dim link As String = ""
Dim description As String = ""
reader.ReadToFollowing("title")
title = reader.ReadElementContentAsString
reader.ReadToFollowing("link")
link = reader.ReadElementContentAsString
reader.ReadToFollowing("description")
description = reader.ReadElementContentAsString
File.AppendAllText(paths1, title + ":" + link + ":" + description + vbCrLf)
Next
End Using
End Sub
End Class</items></item>
这将给你,123.log =项目数+ 1,因为它还计算项目的标题,描述和链接。当你想要计算更多不同的元素时它不是那么可靠,因为有些元素可能不存在11次并且它可能会抛出错误以及混淆数据
你得到这种格式的数据,
标题:链接:描述
你可以使用readalllines函数然后通过':'分隔文本。这是我为旧程序写的分离的ac#代码,
This will be giving you, 123.log = number of items + 1 as it also counts the title, description, link of the item. its not that reliable when you are looking to count more different elements as some elements may not be there 11 times and it could throw a error as well as mix up the data
you get the data in this format,
title:link:Description
which you can use readalllines functions and then separate the text through ':'. Here is a c# code for separating which i wrote for my old program,
string[] strArray = File.ReadAllLines(proxyDB);
int num = 0;
if (strArray.Length >= 1)
{
this.proxy_list = new string[strArray.Length];
foreach (string str in strArray)
{
string[] strArray3 = str.Split(new char[] { ':' });
this.proxy_list[num++] = str;
string proxy = strArray3(0)
string pass = strArray3(1)
}
}
这样你就可以在listview中指定它们,或者你想在foreach中指定它们。
This way you can specify them in listview, or anyway you want inside the foreach.
这篇关于在VB中解析XML项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!