2014-09-19 47 views
1

我想从我指定的xml供稿链接中获取每个项目。这里是XML格式,解析VB中列出的XML项目

<?xml version="1.0" encoding="UTF-8"?> 
<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> 

以下是饲料中的示例项目。对于每个<item></item>,它都有自己的标题,说明和链接。

我希望将标题保留在文本框1中,链接到文本框2中,并在文本框3中仅对第一项进行描述。

大多数情况下,我希望将它们保存为字符串,以便我可以继续使用这些字符串的代码。

任何人都可以帮助我吗?

我已经试过这样的事情:

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://blahblah.com/rss.xml") 
    File.WriteAllText(paths, Source) 

    xDoc.Load(paths) 

    Dim manager As XmlNamespaceManager = New XmlNamespaceManager(xDoc.NameTable) 
    manager.AddNamespace("atom", "http://www.w3.org/2005/Atom") 

    Dim xnList As XmlNodeList = xDoc.SelectNodes("atom:feed/atom:entry", manager) 

    For Each xn As XmlNode In xnList 
     TextBox1.Text = xn.LocalName.ToString() + vbCrLf 
    Next 
End Sub 

这样一来,我已成功地下载整个文件,并将其保存为字符串。但是我不能完成最后一部分。我不明白xml文件是如何工作的,所以我认为这里的错误在原子部分附近。

我想什么是读取XML文件,然后保存每个itemTITLELINKDESCRIPTION在文本文件中。

like,TITLE:LINK:DESCRIPTION
每行一个项目。我上面的示例RSS可以制作2行。

+0

只是为了验证,我想元素,标题,描述和链接在字符串中。否则无关紧要。 – kks21199 2014-09-19 08:19:25

+0

和家伙,我有一个解决方案。如果你允许我,我会发布它。 – kks21199 2014-09-19 15:46:44

回答

0

我找到了2种不同的解决方案。

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中的项目。不会有错误。

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 

这将会给你,123.log =项目+ 1的数,因为它也计算该项目的标题,描述,链接。它不是那么可靠,当你正在寻找更多不同的元素,因为一些元素可能不会在那里11次,它可能会抛出错误以及混淆数据

你会得到这种格式的数据, title:链接:说明

您可以使用readalllines函数,然后通过':'分隔文本。下面是分离的C#代码,我写了我的老节目,

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) 

        } 
       } 

这样你就可以在列表视图指定它们,或者反正你想要的foreach内。