Is there any easy method for splitting text in VB.NET? - 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()

Related

VB.net need help parsing a substring

I have a value I am capturing from an Http Request
Dim someValue As String = Request.Params("search")
Here is the value of my string:
?MyId1=VALUE1&MyId2=VALUE2&MyBoolen=True
I am trying to capture VALUE2. I tried the below code, but haven't had any success.
If Not String.IsNullOrEmpty(someValue) Then
Dim x = someValue.Substring(someValue.IndexOf("&"c) + 1)
If Not String.IsNullOrEmpty(someValue) Then
Dim y = x.Substring(someValue.IndexOf("="c) + 1)
End If
End If
How can I do this?
It looks like you're overthinking this. The Request object will let you look up the MyId2 value directly:
Dim MyId2 As String = Request.QueryString("MyId2")
It's also possible this is a nested query string, where what you actually have is something more like this:
/?search=%3FMyId1%3DVALUE1%26MyId2%3DVALUE2%26MyBoolen%3DTrue
This would give you the original string after the runtime URL Decodes the search element. In that case, you should look at the HttpUtility.ParseQueryString() method, rather than trying to do this yourself:
Dim search As String = Request.Params("search")
Dim searchValues As NameValueCollection = HttpUtility.ParseQueryString(search)
Dim MyId2 As String = searchValues("MyId2")
Which could even be written as a one-liner if we really wanted:
Dim MyId2 As String = HttpUtility.ParseQueryString(Request.Params("search"))("MyId2")
But if you really wanted to parse this by hand, one of the nice things about this is everything should be URL-encoded. This means you don't have worry about stray & or = characters as part of the data, and a simple Split() call should be safe:
Dim MyId2 As String = ""
Dim items = someValue.Substring(1).Split("&"c)
For Each item As String In Items
Dim parts = item.Split("="c)
If parts(0) = "MyId2" Then
MyId2 = parts(1)
Exit For
End If
Next
Or
Dim parts() As String = someValue.Substring(1).Split("&"c).
Select(Function(s) s.Split("="c)).
FirstOrDefault(Function(p) p(0) = "MyId2")
Dim MyId2 As String = If(parts IsNot Nothing, parts(1), "")

How to read attribute value of xml

How can i read attribute value of a xml.
I have this below xml and i am working with windows forms vb.net
I tried something like this below but does not work. I want to read connection string value
Dim xe As XElement = XElement.Load("..\\..\\KMMiddleTier.xml")
Dim name = From nm In xe.Elements("ConnectionKey") Where nm.Element("ConnectionKey").Attribute("Key") = "DB_DEV" Select nm.Element("ConnectionKey").Attribute("ConnectionString").Value.FirstOrDefault()
Dim xmlDoc as XMLDocument
Dim xmlNodeList As XmlNodeList = xmlDoc.SelectNodes("/KMMiddleTierSecurity/ConnectionKeys/ConnectionKey")
Dim strConnectionKey As String = xmlNodeList.Item(0).Attributes("ConnectionString").Value''''
This might help.

VB.Net Cant create "new line" "string"

I am in need of assistance... i am trying to create a textfile with links in it. the code i have..
dim domain as string = "http://www.mywebsite/"
dim name as string = "username"
Dim link As String = New String("domain" & "name")
TextBox1.AppendText(link & Environment.NewLine)
Msgbox(textBox1.lines(0))
The problem is that MsgBox only shows up as "http://www.mywebsite/". the textbox does show "http://www.mywebsite/username" but when copied to text document it is:
Line0: http://www.mywebsite/
Line1:username
any ideas... tried using
Dim link As String = String.Join(domain & name) but that doesnt work nor does
Dim link As String = new String.Join(domain & name)
i need
Msgbox(textBox1.lines(0)) to display "http://www.mywebsite/username" not one or the other.
That was quick got a message saying to use. Dim link As String = String.Concat(domain & name)
i think you should move to StringBuilder first import Imports System.Text
'create a string with multiple lines
Dim a As New StringBuilder
a.AppendLine("hi")
a.AppendLine("there")
a.AppendLine("this")
a.AppendLine("is")
a.AppendLine("a")
a.AppendLine("test")
'read will makes read line by line
Dim read As String() = a.ToString.Split(vbNewLine)
'count has number of lines
Dim count As Integer = a.ToString().Split(vbNewLine).Length - 1
'lines will be added to combobox one by one
For i As Integer = 0 To count - 1
ComboBox1.Items.Add(read(i))
Next
you just should edit it to suits your needs i didnt understand what you needed exactly

How to show rss feed from other source on my page?

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

vb2008 match text between html tags

hello i'm using Visual Basic 2008 Express Edition
how is it possible to match text between tags?
for example i have a string : <data>Text</data>more text..., how i can get the Text which is inside <data></data> ( .Replace won't help).
thanks
My solution :
Public Function parseText(ByVal str As String, ByVal tag As String) As String
Dim match As Match = Regex.Match(str, "<" & tag & "\b[^>]*>(.*?)</" & tag & ">")
If match.Groups.Count = 2 Then
Return match.Groups(1).Value
Else
Return "0"
End If
End Function
I use this because in my case the tags will be always without id, class, width, href, src, style .... just tag name (ex:<data><str><text>...)
You can use RegularExpressions.
Dim s As String = "<data>Hello world</data>"
Dim match As Match = Regex.Match(s, "<data\b[^>]*>(.*?)</data>")
Dim text As String
If match.Groups.Count = 2 Then
text = match.Groups(1).Value
End If
Use the HTML Agility Pack to parse the HTML string and then query the resulting object for the values you want.
The source download comes with many example projects.
This may help you
Dim findtext2 As String = "(?<=<data>)(.*?)(?=</data>)"
Dim myregex2 As String = TextBox1.Text 'Your HTML code
Dim doregex2 As MatchCollection = Regex.Matches(myregex2, findtext2)
Dim matches2 As String = ""
For Each match2 As Match In doregex2
matches2 = matches2 + match2.ToString + Environment.NewLine
Next
MsgBox(matches2) 'Results
Don't forget Imports System.Text.RegularExpressions.
Above code is getting all information between 2 strings, in this case - <data> and </data>. You can use whatever you want (it doesn't need to be tag, not even html).