Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im making an RSS Feed reader, in which I categorize feeds, and per category has a list of different RSS Feeds that the user will be able to customize. I want categorize all the feeds inside a single text file, for easy editing, and get every line between <category1></category1>injected into a combo box. Example:

<category1>
feed1
feed2
feed3
</ccategory1>
<category2>
feed4
feed5
</category2>

With the combobox for Category1 only recieiving the text:
feed1
feed2
feed3

While the same combobox is filled with Category 2 upon selection, looking like this:
feed4
feed5


What I have tried:

Google, never been good with StreamReaders/Writers
Posted
Updated 3-May-20 2:35am
Comments
MadMyche 2-May-20 7:37am    
It would be helpful to see the code... makes it a lot easier to see what may be wrong with it
Richard MacCutchan 2-May-20 7:39am    
If that is real XML then you can use the XMLDocument class to read the content. If it is just a text file with XML-like keywords then use the StreamReader class and check for the keyword items as you read each line. What is the actual problem?
Member 12111727 2-May-20 7:58am    
I have no problems with my XML streamer, its the text file streaming. Im not sure how to find a chunck of text, in this case: a chunck between <category1> and <_/category1>

Would it be easier to write and read to an xml rather than a text file?
Richard MacCutchan 2-May-20 8:01am    
It may be easier to write it to XML but it all depends on what you are trying to do. Reading a simple text file and checking for keyword entries of the form shown above is a fairly simple process.
Member 12111727 2-May-20 8:04am    
Thats all I want to do. Basically I have a Combo box and a DataviewGrid. My RSSParser class takes care of translating known RSS feeds and listing the items in the DataViewGrid, All i want to do, is be able to select with the combo box, which feed I want to populate the GridView with.

But I also want the user to be able to customize which Feeds are available, hence the textfile with categories and feed links

You don't really need to deal with stream reader:
VB.NET
Public Shared Function GetFeeds(ByVal doc As XmlDocument, ByVal category As String) As String()

   If (doc Is Nothing) Then 
      Throw New ArgumentNullException(NameOf(doc))
   End If

   Dim node As XmlElement = CType(doc.DocumentElement.SelectSingleNode("//" + category), XmlElement)

   If (node IsNot Nothing) Then
      Return node.InnerText.Split(New String() { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
   Else
      Return New String() { }
   End If

End Function

'' Usage
Dim doc As XmlDocument = XmlDocument.Load("path\to\your\xml\file")
Dim feeds1 As String() = GetFeeds(doc, "category1")
Dim feeds2 As String() = GetFeeds(doc, "category2")

comboBox1.Items.AddRange(feeds1)
combobox2.Items.AddRange(feeds2)

There may be some mistakes as I used a c# to vb.net converter (I don't write vb.net code anymore), but this is a concise way to get your combobox items from your xml file.
 
Share this answer
 
Comments
Member 12111727 2-May-20 8:10am    
Only issue I am having is that XmlDocument doesnt have a .Load function
phil.o 2-May-20 8:25am    
You may have to add a reference to System.Xml.dll.
The Load method really exists:
XmlDocument.Load Method
Member 12111727 2-May-20 9:03am    
Ok, it fixed that, but now its just not working, no errors. I had to slightly odify the code because Document Element wasnt workin, so i switched it to Elements

Public Function GetFeeds(ByVal FeedsDoc As XDocument, ByVal category As String) As String()

If (FeedsDoc Is Nothing) Then
Throw New ArgumentNullException(NameOf(FeedsDoc))
End If

Dim node As XmlElement = CType(FeedsDoc.Elements("/category/" + category + "/link"), XmlElement)

If (node IsNot Nothing) Then
Return node.InnerText.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Else
Return New String() {}
End If

End Function
Member 12111727 2-May-20 9:05am    
My RSS Feed XML



<category>
<earthquake>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</earthquake>
<volcano>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</volcano>
<hurricane>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</hurricane>
<tsunami>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</tsunami>
<space>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</space>
<disease>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</disease>
<custom>
<link>https://www.volcanodiscovery.com/volcanonews.rss</link>
</custom>
</category>

phil.o 2-May-20 9:20am    
I wrote XmlDocument, you wrote XDocument. Those are distinct objects whose usages are also distinct.
I Got it. Turns out the problem was how I was calling and using my nodes and xmlDocument

Public Function FillFeedList(combo As ComboBox, category As String)
        'Connect to Internal Feed Database
        Dim path As String = "RSSFeeds.xml"
        Dim feedDoc As New XmlDocument()
        feedDoc.Load(path)

        Dim selectedCategory As XmlNode = feedDoc.SelectSingleNode("category/" + category)
        Dim elements As New List(Of String)
        For i As Integer = 0 To selectedCategory.ChildNodes.Count
            Dim feedLink As XmlNode = selectedCategory.ChildNodes.Item(i)
            If feedLink IsNot Nothing Then
                elements.Add(feedLink.InnerText)
            End If
        Next
        combo.Items.AddRange(elements.ToArray())
        Return Nothing


    End Function
 
Share this answer
 
v2

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