Problems with using .InnerHtml in ASP.NET c # - c#

I have a div in the aspx page which i gave the attribute runat = "server" and id = "content".
When starting the page I need to create some cards with data taken from a database. But it gives me error when I try from code behind in c # using content.InnerHtml to add html code.
aspx page
<div class="page-wrapper">
<div class="page-content" runat="server" id="contenuto">
</div>
</div>
code behind
contenuto.InnerHtml += "<div class='row'>";
it gives me this error: "Could not get the internal content of content because it is not in literal format."
image of the Error

"contenuto" not containing any server control inside it, you required to change your code like below -
var sb = new StringBuilder();
String innertext =sb.Append("Text what you required or tag");
contenuto.InnerHtml = innertext ;

Related

Problem with subnode's text when scraping data from a website with HtmlAgilityPack

Hope somebody can help this newbie.
I tried many paths for this subnodes but i cant figure it out.
Html part:
<div class="center-block"> == $0
<div class="match-time" id="dvStatusText">MS</div>
<div class="match-score" id="dvScoreText">4 - 0</div>
<div class="hf-match-score" id="dvHTScoreText">İY : 3- 0</div>
</div>
My code:
Uri url = new Uri("http://arsiv.mackolik.com/Mac/3213138/");
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
try
{
string html = client.DownloadString(url);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection results = doc.DocumentNode.SelectNodes("//*[#class='center-block']"); //
if (results != null)
{
for (int i = 0; i < results.Count; i++)
{
var t1 = results[i].SelectSingleNode("//*[#class='match-score']").InnerText; // (FT)
var t2 = results[i].SelectSingleNode("//*[#id='dvHTScoreText']").InnerText; // ht
listBox1.Items.Add(t2.ToString());
}
}
My problem from InnerHtml result:
<div class="match-time" id="dvStatusText">MS</div>
<div class="match-score" id="dvScoreText">4 - 0</div>
<div class="hf-match-score" id="dvHTScoreText"></div> // this element has always contains text.
I tried different ways to solve this problem but i have nothing. I can scrape "class=match time" or "class=match-score". But i cant "class=hf-match-score" . I have tried scrape with class or id. Different ways same problem.
Please show me a way. Thanks alot.
The score at half-time is displayed with Javascript. You'll need Selenium or a similar tool to access this element.
As an alternative, you can fetch the data directly from the JSON loaded in the background.
Piece of code in Python (I suppose you can do the same in c#) :
import requests
from lxml import html
# We set up the download url (obtained in the network tab of the developer tool) and the mandatory header
url = 'http://arsiv.mackolik.com/Match/MatchData.aspx?t=dtl&id=3213138&s=0'
hd = {'Referer': 'http://arsiv.mackolik.com/Mac/3213138/'}
# We download and parse the json
data = requests.get(url,headers=hd)
val= data.json()
# We extract values of interest
print(val["d"]["s"],val["d"]["ht"],sep="\n")
Output :
4 - 0
3 - 0

Xpath to get href that contain id?

I am trying to get all links that contain ids.I have tried for name and price which is working perfectly but not able to get links which is related to that stuff.
For name I am using this code but for getting links it is not working.
//For Name
var name=scorenodesdoc.DocumentNode.SelectNodes("//[contains(#id,'item')]/ul[1]/li1]/span");
//for Links
var Links= doc.DocumentNode.SelectNodes("//a[contains(#id, 'item')]/#href");
xpath for link is://*[#id="item5d86882c07"]/div[1]/div/a
//This is the code I am try to get the href link
<li id="item5d86882c07" _sp="p2045573.m1686.l8" listingid="401689029639" class="sresult lvresult clearfix li" r="1">
<div class="lvpic pic img left" iid="401689029639">
<div class="lvpicinner full-width picW">
<a href="https://www.ebay.com/itm/Microsoft-Xbox-One-X-White-Console-1TB-Forza-Special-Edition-Bundle-White/401689029639?hash=item5d86882c07:g:lgwAAOSwoZJcQY5s" class="img imgWr2">
<img src="https://i.ebayimg.com/thumbs/images/g/lgwAAOSwoZJcQY5s/s-l225.jpg" class="img" alt="Microsoft Xbox One X White Console 1TB & Forza Special Edition Bundle - White'">
</a>
</div>
</div>
</li>
Ok this is how I resolve my issue.First it gets get anchor tag information then by using getattributevalue to get value of href.
var URLnodes = doc.DocumentNode.SelectNodes("//*[contains(#id,'item')]/div[1]/div/a");
var AllURL = URLnodes.Select(node => node.GetAttributeValue("href",null));

How to get div without class with Html Agility Pack

I try to get 'Galatasaray.' from following HTML. I don't know how can I specified that
<div class="dRib1">
<h2>Bilgi</h2>
</div>
<div>Galatasaray.</div>
HtmlNodeCollection aciklamaCek = dokuman.DocumentNode.SelectNodes("//div[#class='dRib1']");
textBox8.Text = aciklamaCek[0].NextSibling.NextSibling.InnerText.Trim();
I solved my problem with this code.

Reading a textstring from a webpage

Currently I'm trying to read out a text from a Website via a c# program.
To be exact the Track and the Dj from www.hardbase.fm.
This is what the page source looks like:
<div id="Moderator">
<div id="Moderator_special">
<div style="width:158px; float:left; margin:8px"></div>
<div id="onAir" style="width:420px;overflow:hidden;">
<strong>
<a href="/member/46069" target="_top">
<span style="color:#4AA6E5">BIOCORE</span>
</a>
<span style="color:#26628B"> mit "This Is BIOCORE" (Hardstyle)</span>
</strong>
</div>
</div>
</div>
The text I want to read out is "BIOCORE" and "mit "This Is BIOCORE" (Hardstyle)"
(the text seen when running the snippet).
If have tried the following:
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.hardbase.fm/");
first = webData.IndexOf("#4AA6E5\">") + "#4AA6E5\">".Length;
last = webData.LastIndexOf("</span></a><span style=\"color:#26628B\">");
hb_dj = webData.Substring(first, last - first);
But this doesn't always works because sometimes the source code of the page changes a bit. Like the color or so. And then the search wont work.
So the question is: Is there a better method to do this?
You should try the HTML Agility Pack
HtmlWeb page = new HtmlWeb();
HtmlDocument document = page.Load("http://www.hardbase.fm/");
var nodes = document.DocumentNode.SelectNodes("//[#id='onAir']");
var nodes2 = nodes.Select(c1 => c1.SelectNodes("span")).ToList();
var span1=nodes2[0];
var span2 nodes2[1]

How do I get web browser element's css style?

I have a page like the below in my web browser that I want to get the style atributte value. I tried:
HtmlElement ele = webBrowser1.Document.GetElementById("foo");
MessageBox.Show(ele.GetAttribute("style"));
but it output:
System.__ComObject
Why does it output a System.__ComObject type and how do I handle it?
HTML page:
<div id="foo" style="display:block;">
a
</div>
ele.Style
Will help.
ele.GetAttribute("Style")
won't work because returns string, so it can't say more than that is an object, while ele.Style returns CssStyleCollection.
var e = document.getElementById('foo');
var css = window.getComputedStyle(e,null).getPropertyValue("display");
alert(css);

Categories