Click here to Skip to main content
15,793,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to get each items from xml feed link i specify. Here is the XML format,

XML
<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
XML
<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 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 :
VB
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


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.
Posted
Updated 30-Sep-14 2:38am
v5
Comments
Sergey Alexandrovich Kryukov 19-Sep-14 4:09am    
Why, why showing XML as a bitmap? Who is going to look at it? With closed nodes?! Is that so difficult to copy and past the text?
Now, what's wrong with reading original MSDN documentation on XML parsing? Please...
—SA

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:
  1. 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[^].
  2. 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[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, 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[^].


—SA
 
Share this answer
 
Comments
KKS21199 19-Sep-14 5:22am    
can you see my updated question. I have read that, but i don't understand how to get elements in each item seperately
KKS21199 19-Sep-14 6:05am    
Here,
Dim xmldoc As New XmlDocument()
xmldoc.Load(xmlreader)


Dim xDoc As XmlDocument = New XmlDocument()
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:item", manager)

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

I have got it in c# and have written it in VB. but my problem is i don't understand how xml works. So i am sure the atom part is wrong. Can you check my rss code upthere and help me
Sergey Alexandrovich Kryukov 19-Sep-14 10:32am    
This way of parsing is hard-coded, "atom", "atom:item" are immediate constants. This is not supportable. And XML... I don't know what to say. It's trivial. Okay, just try harder... :-)
—SA
I have found 2 different solutions.

Solution 1 :
VB
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.

Solution 2 :
VB
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>


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,
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)

                   }
               }


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900