Get a specific part of style attribute in VB.NET - vb.net

If I used .GetAttribute("style") and it returned:
width:200px;height:300px;background-image:url('http://someurl.com/image.png");position:absolute;
How would I go about getting the background-image URL?
[ EDIT ]
I should also mention that the background-image changes every now and again, it doesn't stay the same.
[ EDIT ] I am trying to pull the background image from bing in a webbrowser. I want to set the background as my form background.
[EDIT]
Try
With bingCheck
Dim bgDiv As HtmlElement = .Document.GetElementById("bgDiv")
Dim imgUrl As String = bgDiv.Style("background-image").ToString
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(imgUrl), HttpWebRequest)
Dim response As Net.HttpWebResponse = DirectCast(request.GetResponse, Net.HttpWebResponse)
Dim grabbedImage As Image = Image.FromStream(response.GetResponseStream)
response.Close()
Me.BackgroundImage = grabbedImage
Me.Update()
End With
Catch ex As Exception
End Try

Many controls provide built-in Style collection. So you can do something like:
Dim sUrl As String = xMyControl.Style("background-image")

Dim str = "width:200px;height:300px;background-image:url('http://someurl.com/image.png');position:absolute;"
Dim v = "background-image:url("
Dim i = str.IndexOf(v) + v.Length + 1
Dim j = str.IndexOf(")", i)
Dim url = str.Substring(i, j - i - 1)

Related

VB.NET SetAttribute in WebBrowser doens't work

I did some research previously, but all the answers do not work.
The "value" attribute exists in the element but does not appear in the webBrowser, nor in the input.
This is my code until then, I need the webBrowser to read an html file, then load your answers or values ​​from your inputs from a database.
PS:
My application is built in real time, there is no webbrowser control on the screen, it is created shortly after reading the html file and only then it is placed inside a panel.
Dim webBrowser As WebBrowser = New WebBrowser
Dim _doc As HtmlDocument
Dim htmlPath As String = "C:\ePrimeCare\Platis\Debug\Protocolos\" +
nomeProtocolo + "_" + idSistema.ToString() + ".html"
webBrowser.ScriptErrorsSuppressed = True
webBrowser.Navigate(htmlPath)
_doc = webBrowser.Document.OpenNew(False)
'webBrowser.DocumentText = IO.File.ReadAllText(htmlPath).ToString()
'webBrowser.Document.OpenNew(False)
'RetornaRespostasAnteriores(idSistema, idFicha, nomeProtocolo, _doc, Convert.ToDateTime(dtVisita))
_doc.Title = nomeProtocolo
_doc.Write(IO.File.ReadAllText(htmlPath).ToString())
Dim carregaRespostas As CarregarRespostaProtocoloHTML = New CarregarRespostaProtocoloHTML
Dim respostas As DataTable = carregaRespostas.BuscarRespostasProtocoloAnterior(idFicha, idSistema, dtVisita)
Dim idopcaoitem As String = 0
Dim idsetitem As String = 0
Dim value As DataRow()
Dim strArr As String()
For Each element As HtmlElement In _doc.GetElementsByTagName("input")
Dim type As String = element.GetAttribute("type")
Select Case type
Case "text"
strArr = element.GetAttribute("id").Split("_") 'For get the two ids
idopcaoitem = strArr(0)
value = respostas.Select(("IDOPCAOITEM = " + idopcaoitem.ToString()))
If value.Length > 0 Then
element.SetAttribute("value", value(0)(2).ToString())'Here i try to set the value, but does not work
End If
Case "radio"
Debug.WriteLine("Input de radio")
Case "checkbox"
Debug.WriteLine("Input de checkbox")
Case "hidden"
Debug.WriteLine("Input de hidden")
Case Else
Debug.WriteLine("Outro input")
End Select
Next
_doc.Write(IO.File.ReadAllText(htmlPath).ToString())
webBrowser.Refresh(WebBrowserRefreshOption.Completely)
webBrowser.Dock = Dock.Fill
pnlMain.Controls.Add(webBrowser)
All your document rewriting and the refresh you do at the end will overwrite any changes you made to it.
'Either of these will revert the document back to its original state.
_doc.Write(IO.File.ReadAllText(htmlPath).ToString())
webBrowser.Refresh(WebBrowserRefreshOption.Completely)
You don't even need to call _doc.Write() as WebBrowser1.Navigate(htmlPath) will work just as fine.
New code:
Dim webBrowser As New WebBrowser 'Shorthand statement.
Dim _doc As HtmlDocument
Dim htmlPath As String = "C:\ePrimeCare\Platis\Debug\Protocolos\" +
nomeProtocolo + "_" + idSistema.ToString() + ".html"
webBrowser.ScriptErrorsSuppressed = True
webBrowser.Navigate(htmlPath)
_doc = webBrowser.Document 'Removed OpenNew().
_doc.Title = nomeProtocolo
Dim carregaRespostas As New CarregarRespostaProtocoloHTML 'Another shorthand statement.
Dim respostas As DataTable = carregaRespostas.BuscarRespostasProtocoloAnterior(idFicha, idSistema, dtVisita)
(...your variables...)
For Each element As HtmlElement In _doc.GetElementsByTagName("input")
(...your code...)
Next
'(Removed _doc.Write() and Refresh() since they will undo all changes)
webBrowser.Dock = Dock.Fill
pnlMain.Controls.Add(webBrowser)

HtmlAgilityPack - SelectNodes

I'm trying to retrieve a <p class> element.
<div class="thread-plate__details">
<h3 class="thread-plate__title">(S) HexHunter BOW</h3>
<p class="thread-plate__summary">created by Aazoth</p> <!-- (THIS ONE) -->
</div>
But with no luck.
The code I am using is below:
' the example url to scrape
Dim url As String = "http://services.runescape.com/m=forum/forums.ws?39,40,goto," & Label6.Text
Dim source As String = GetSource(url)
If source IsNot Nothing Then
' create a new html document and load the pages source
Dim htmlDocument As New HtmlDocument
htmlDocument.LoadHtml(source)
' Create a new collection of all href tags
Dim nodes As HtmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//p[#class]")
' Using LINQ get all href values that start with http://
' of course there are others such as www.
Dim links =
(
From node
In nodes
Let attribute = node.Attributes("class")
Where attribute.Value.StartsWith("created by ")
Select attribute.Value
)
Me.ListBox1a.Items.AddRange(links.ToArray)
Dim o, j As Long
For o = 0 To ListBox1a.Items.Count - 1
For j = ListBox1a.Items.Count - 1 To (o + 1) Step -1
If ListBox1a.Items(o) = ListBox1a.Items(j) Then
ListBox1a.Items.Remove(ListBox1a.Items((j)))
End If
Next
Next
For i As Integer = 0 To Me.ListBox1a.Items.Count - 1
Me.ListBox1a.Items(i) = Me.ListBox1a.Items(i).ToString.Replace("created by ", "")
Next
For Each s As String In ListBox1a.Items
Dim lvi As New NetSeal.NSListView
lvi.Text = s
NsListView1.Items.Add(lvi.Text)
Next
It runs but I can't get the 'created by XXX' text.
I've tried many ways but got no luck, an hand would be appreciated.
Thanks in advance everyone.
Looks like you are looking wrong string in the attribute.Value. What I see is that attribute.Value.StartsWith("created by ") must be changed to this one attribute.Value.StartsWith("thread-plate__summary").
And to grab inner content of node you have to do this: Select node.InnerText;
' the example url to scrape
Dim url As String = "http://services.runescape.com/m=forum/forums.ws?39,40,goto," & Label6.Text
Dim source As String = GetSource(url)
If source IsNot Nothing Then
' create a new html document and load the pages source
Dim htmlDocument As New HtmlDocument
htmlDocument.LoadHtml(source)
' Create a new collection of all href tags
Dim nodes As HtmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//p[#class]")
' Using LINQ get all href values that start with http://
' of course there are others such as www.
Dim links =
(
From node
In nodes
Let attribute = node.Attributes("class")
Where attribute.Value.StartsWith("thread-plate__summary")
Select node.InnerText
)
Me.ListBox1a.Items.AddRange(links.ToArray)
Dim o, j As Long
For o = 0 To ListBox1a.Items.Count - 1
For j = ListBox1a.Items.Count - 1 To (o + 1) Step -1
If ListBox1a.Items(o) = ListBox1a.Items(j) Then
ListBox1a.Items.Remove(ListBox1a.Items((j)))
End If
Next
Next
For i As Integer = 0 To Me.ListBox1a.Items.Count - 1
Me.ListBox1a.Items(i) = Me.ListBox1a.Items(i).ToString.Replace("created by ", "")
Next
For Each s As String In ListBox1a.Items
Dim lvi As New NetSeal.NSListView
lvi.Text = s
NsListView1.Items.Add(lvi.Text)
Next
I hope this will work for you.

ITextSharp PdfReader not reading new text in different PDFs

I have a windows services application that reads the text of PDFs using ITextSharp. I'm using a textbox to show the text of PDFs.
It works fine when it reads the first PDF, but when it reads the second PDF, the text does not change and the text is still of the first PDF. Here is my code :
dim vFileName as string
dim vFileEntries as string()
dim vPath as string = "C:\PDF"
if directory.exists(vPath) then
vFileEntries = directory.getfiles(vPath)
for each vFileName in vFileEntries
dim PR as PdfReader = new PdfReader(vFileName)
for CurrentPage as integer = 1 to PR.NumberOfPages
RichTextBox1.text = ""
dim ltestrategy as LocationTextExtractionStrategy = New LocationTextExtractionStrategy
dim currentext as string = PdfTextExtractor.GetTextFromPage(PR, CurrentPage, ltestrategy)
RichTextBox1.Text = RichTextBox1.Text + currentext
next
PR.close()
next vFileName
end if
Any help is appreciated
The way your code is set up now, it look like RichTextBox1.text will contain the text of the last page of the last pdf that got processed. The following change will bring in the text from all pages of all pdfs processed from your folder.
To make this happen you will want to change the following:
for CurrentPage as integer = 1 to PR.NumberOfPages
RichTextBox1.text = ""
dim ltestrategy as LocationTextExtractionStrategy = New LocationTextExtractionStrategy
dim currentext as string = PdfTextExtractor.GetTextFromPage(PR, CurrentPage, ltestrategy)
RichTextBox1.Text = RichTextBox1.Text + currentext
next
to:
for CurrentPage as integer = 1 to PR.NumberOfPages
currenttext = ""
dim ltestrategy as LocationTextExtractionStrategy = New LocationTextExtractionStrategy
dim currentext as string = PdfTextExtractor.GetTextFromPage(PR, CurrentPage, ltestrategy)
RichTextBox1.Text = RichTextBox1.Text + currentext
next
where you are re-initializing currentext instead of RichTextBox1.text. This will give you the text of all of the pdfs, with all of their pages, to the text box.

Affiliate Window API passing columns to Service Causes Application Crash (VB.net)

This generates a crash, and I have no idea why?
I have very little experience using SOAP/WSDL and I think this may be why I have no idea how to even start to debug this.
Sub Main()
Dim service As AWIN.ApiService = New AWIN.ApiService
Dim columns As AWIN.getProductList
Dim AWresults() As AWIN.Product
Dim response As New AWIN.getProductListResponse
Dim total As Integer
Dim activerefine As AWIN.RefineByGroup
Dim refine As AWIN.RefineByGroup
Const token As String = "xxy"
Dim UA As AWIN.UserAuthentication = New AWIN.UserAuthentication
With UA
.sApiKey = token
End With
service.UserAuthenticationValue = UA
columns = New AWIN.getProductList
Dim stringsofthings As String() = {"sId", "iCategoryId", "iMerchantId", "sMerchantProductId", "iAdult", "bHotpick", _
"iUpc", "iEan", "sMpn", "iIsbn", "sName", "sDescription", "sSpecification", _
"sPromotion", "sBrand", "sModel", "sAwDeepLink", "sAwThumbUrl", "sAwImageUrl", _
"sMerchantThumbUrl", "sMerchantImageUrl", "sDeliveryTime", "fPrice", "sCurrency", _
"fStorePrice", "fRrpPrice", "fDeliveryCost", "bWebOffer", "bPreOrder", "sWarranty"}
columns.sColumnToReturn = stringsofthings
response = service.getProductList(columns)
For c = 0 To UBound(response.oProduct)
ReDim Preserve AWresults(c)
AWresults(c) = New AWIN.Product
AWresults(c) = response.oProduct(c)
Console.WriteLine(AWresults.ToString)
Next
Console.ReadLine()
End Sub

why I can't set nothing to variable in vb.net?

I have a a problem with my vb.net.
my code
class business{
buiding as string
}
I load data from mongodb
Dim collection1 = db1.GetCollection(Of business)("tablebusiness")
Dim list = collection1.Find(query1)
For Each abiz In list
Dim biztemp = abiz
biztemp.buiding = nothing '//// (but I get biztemp.building = "") why??
'biztemp.building = "" here
Next