1
votes

I want to get each items from xml feed link I specify. Here is the XML format,

<?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>

Here is a sample item in a feed. For each <item></item> 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 description 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?

I have tried something like this:

   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  

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 don't understand how xml file works, so I think the mistake here is near the atom part.

What I wanted was to read a XML file, then save each item, TITLE, LINK and DESCRIPTION in a text file.

like, TITLE:LINK:DESCRIPTION
one in each line for one item. My example RSS above can make 2 lines.

1
Just to verify, i want the element, title, description and link to be in the string. Else doesn't matter.kks21199
and guys, I got a solution. If you will allow me, I will post it.kks21199

1 Answers

0
votes

I have found 2 different solutions.

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

This way, you get the number of lines in the 123.log file = items in rss. There will be no errors.

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

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)

                    }
                }

This way you can specify them in listview, or anyway you want inside the foreach.