How to show rss feed from other source on my page? - vb.net

Hi i am collecting a lot of information from different websites and putting them together for personal use.
there is a rss feed link.
http://picasaweb.google.com/data/feed/base/all?alt=rss&kind=photo&access=public&filter=1&q=waldorf+hilton&hl=en_US
How can i use it? in case of html i just take them and assign them to literals .But how can i display this .
Here is my code..
Dim rssFeed As HttpWebRequest = DirectCast(WebRequest.Create("http://picasaweb.google.com/data/feed/base/all?alt=rss&kind=photo&access=public&filter=1&q=waldorf+hilton&hl=en_US"), HttpWebRequest)
Dim rssData As DataSet = New DataSet()
rssData.ReadXml(rssFeed.GetResponse().GetResponseStream())
Dim Title, Description As String
Dim channelItems As Object() = rssData.Tables(2).Rows(0).ItemArray
Dim titleColumn As Integer = rssData.Tables(2).Columns("title").Ordinal
Dim descriptionColumn As Integer = rssData.Tables(1).Columns("description").Ordinal
Title = channelItems.GetValue(titleColumn).ToString()
Description = channelItems.GetValue(descriptionColumn).ToString()
Repeater1.DataSource = rssData.Tables(2)
Repeater1.DataBind()
Thank you

Are you trying to do this on the web page or inside a vb.net application?
For a webpage: Parse RSS with jQuery
For vb.net, here's one solution: http://www.go4expert.com/forums/showthread.php?t=4577

Related

VB Httpwebresponse get content

On the following webpage I'd like to get all the titles of youtube videos in my listbox1
Dim webRequest As WebRequest = webRequest.Create("https://www.youtube.com/results?q=test")
Dim webresponse As WebResponse = webRequest.GetResponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(webresponse.GetResponseStream())
Dim youtube As String = sr.ReadToEnd
Dim r As New System.Text.RegularExpressions.Regex("title="".*""")
Dim matches As MatchCollection = r.Matches(youtube)
For Each itemcode As Match In matches
ListBox1.Items.Add(itemcode.Value.Split("""").GetValue(1))
However with this code I get the titles but also a bunch of other stuff...
YouTube provides an API which might be a better way to get this information. The specific call you want to make is documented here: https://developers.google.com/youtube/v3/docs/search/list.
In order to use the YouTube API you will need to create an API key. This can be done from the Google developers console. Once you have a key then you can make calls to YouTube to search videos, get video information etc
Using your code as the basis you could use something along these lines:
Dim url As String = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=test&maxResults=50&key={YOUR-API-KEY}"
Dim webRequest As WebRequest = webRequest.Create(url)
Dim webresponse As WebResponse = webRequest.GetResponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(webresponse.GetResponseStream())
Dim youtube As String = sr.ReadToEnd
Dim r As New System.Text.RegularExpressions.Regex("""title"": "".*""")
Dim matches As MatchCollection = r.Matches(youtube)
For Each itemcode As Match In matches
ListBox1.Items.Add(itemcode.Value.Split(":").GetValue(1).Trim().TrimStart("""").TrimEnd(""""))
Next
The q parameter specifies the search query. This will get the first 50 matches to your search and put them in your drop down list.
If you want to stick with regular expression try the following
Dim r As New System.Text.RegularExpressions.Regex("title=""([^""]*)""")
Dim matches As MatchCollection = r.Matches(youtube)
For Each itemcode As Match In matches
ListBox1.Items.Add(itemcode.Groups(1))
Next
However a dedicated API is cleaner

How to extract data from a website in VB.net using XPATH or other techniques?

I am trying to write a simple app in VB.net to gather information from a website and then have the app email the results to me. The purpose is to gather page counts from a printer when the toner is changed. I have the XPATH of the data I need, but I have not been able to figure out how to use this in VB. (I have very little experience programming).
So far I have the app logging into the printer portal and displaying the webpage that has the information I need. The XPATH for this info is:
//*[#id="contents"]/div[3]/div/table/tbody/tr[1]/td
Can anyone help me extract and parse the number out of this table cell?
Thanks for any help you guys can give!!
you can use streamreader! look ath this example:
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
webRequest = webRequest.Create("YOUR URL TO THE PAGE GOES HERE")
webresponse = webRequest.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
Dim sourcecode As String = inStream.ReadToEnd()
Dim x1 As Integer = sourcecode.IndexOf("<td>Nr. of toner changed:") + 25 '25 is the number of characters in the string to search
Dim x2 As Integer = sourcecode.IndexOf("times </td>") 'what comes right after your search string. has to be unique in the page
dim nuberofchanges as integer = sourcecode.Substring(x1, x2 - x1)
hope this helps! :)

Visual Basic set User Agent with ReadXml

I'm trying to set the user agent for a request with XmlRead. I googled a lot about this and couldn't find the answer. Here is my chunk of code:
Dim RssData As DataSet
Dim Title As String
Dim Url As String
Dim Stream As String
Dim buffer As Integer
RssData = New DataSet()
RssData.ReadXml("http://localhost/user_agent.php")
buffer = 0
For Each RssRow As DataRow In RssData.Tables("entry").Rows
Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
Stream += Title & vbCrLf
Next
LinkLabel3.Text = Stream
For Each RssRow As DataRow In RssData.Tables("entry").Rows
Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
Url = RssRow.Item("url").ToString
LinkLabel3.Links.Add(buffer, Title.Length, Url)
buffer = buffer + Title.Length + 2
Next
The part of the code that actually performs the web request is buried pretty deep so you'd have to inherit a bunch of code to do what you asked for. Instead, let me suggest a different path, download the XML on your own with code that's easy to set that header, and then load that into the dataset. The WebClient class lets you set arbitrary headers and has a simple DownloadString method. Once you've got that you can wrap it in a MemoryStream and pass that into ReadXml(). (I couldn't find a way to read the XML as a string, that's why I was forced to read it as Stream.)
''//Will hold our downloaded XML
Dim MyXml As String
''//Create a webclient to download our XML
Using WC As New System.Net.WebClient()
''//Manually set the user agent header
WC.Headers.Add("user-agent", "your user agent here")
''//Download the XML
MyXml = WC.DownloadString("http://localhost/user_agent.php")
End Using
''//Create our dataset object
Dim RssData As New DataSet()
''//There is no direct method to load XML as a string (at least that I could find) so we will
''// convert it to a byte array and load it into a memory stream
Dim Bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(MyXml)
Using MS As New System.IO.MemoryStream(Bytes)
''//Load the stream into the reader
RssData.ReadXml(MS)
End Using
''//Your code continues normally here

Is there any easy method for splitting text in VB.NET?

Is there any easy method for splitting text in VB.NET? (using a start and end string to grab whats in between?)
I do this all the time in JScript with the following:
<junk>
<blah>
<data>someData1</data>
<data>someData2</data>
<data>someData3</data>
</blah>
</junk>
var data = string.split('<data>')[1].split('</data>')[0];
would give me "someData1" by changing the [1] index to [2] would give me "someData2" very easy
for some reason this seems to be very difficult to achieve in VB.NET.
Here is a chunk of the actual HTML I'm dealing with:
<...malformed html>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:48'>Total</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:346.2141'>18,072.59</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:444.3433'>100.00%</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:567.1293'>21,687.11</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:666.3433'>100.00%</div>
<malformed html...>
I need to find the <div>Total</div> index then grab the data between the 1st and 3rd divs after that.
Dim e = XElement.Parse(str)
Dim a = e.XPathSelectElements("./blah").Elements().ToArray()
a(0).Value 'someData1
a(1).Value 'someData2
EDIT:
To parse html try using the Html Agility Pack
I got it working, although this is some of the worse code I've ever written...
Dim sr As StreamReader
sr = New StreamReader("C:\test.html")
Dim xactHTML As String = sr.ReadToEnd
Dim left As Integer = xactHTML.IndexOf("Total</div>")
Dim chunk1 As String = xactHTML.Substring(left + 12)
Dim right As Integer = chunk1.IndexOf("<div style='position")
Dim chunk2 As String = chunk1.Substring(0, right - 1)
Dim xHTML As String = "<xml>" & chunk2 & "</xml>"
Dim e = XElement.Parse(xHTML)
Dim a = e.Elements().ToArray()
Dim damageAmmount As String = a(2).Value()

Deserialize a Digital Persona template in VB.net

Reading binary data out of the database, and I need to convert it back into a Digital Persona fingerprint template. I'm not familiar with serialization and deserialization, so I could use a bit of help. Here's what I tried:
Dim rsBioData As SqlDataReader = SQL.ExecuteReader
Dim byteTemplate As Byte
Dim memStreamTemplate As MemoryStream
If rsBioData.HasRows Then
While rsBioData.Read
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template = DirectCast(template.DeSerialize(memStreamTemplate), DPFP.Template)
End While
End If
rsBioData.Close()
I receive an error that template.DeSerialize(memStreamTemplate) does not create a value.
For kicks, here's how I serialized the object to place it into the database. I assume this part is working, since the binary data shows up in SQL server--just can't read it back out to see.
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
SQL.Parameters.AddWithValue("biometricData", serializedTemplate)
Thanks
Here's how I was finally able to do it. I was SO close the first time around.
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template.DeSerialize(memStreamTemplate)