This is a migrated thread and some comments may be shown as answers.

HtmlAgilityPack problem DataSource

1 Answer 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jakub
Top achievements
Rank 1
Jakub asked on 07 Sep 2015, 03:29 PM

Dear Telerik Team,

I want to import data from different websites with HTML Agility Pack. Then I want to export data to my SQL database. I have little problem because every cell return my "&nbsp" . I use method from this website:
http://www.telerik.com/forums/access-cell-values-in-radgrid-selected-index-event

So I think that problem is in import data.

I attach 2 sample code."Row" file return me correct data, but return me the same problem. I think that "Cell" sample code should return me correct data but in every rows display the same information. 

Cell Code: 

    WebClient webClient = new WebClient();
            StreamReader page = new StreamReader(WebRequest.Create("http://nolimits.art.pl/grafik/wyswietl.php?typ=2").GetResponse().GetResponseStream(), Encoding.UTF8);

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(page);

            HtmlNode node = doc.DocumentNode.SelectSingleNode("//table");
            string context = node.InnerHtml;
            DataTable NLTable = new DataTable();
            //NLTable.Columns.Add(" ");//CheckBox
            NLTable.Columns.Add("Dzien tygodnia");
            NLTable.Columns.Add("Godziny zajec");
            NLTable.Columns.Add("Nazwa zajec");
            NLTable.Columns.Add("Poziom");
            NLTable.Columns.Add("Instruktor");
            NLTable.Columns.Add("Wolne miejsce dla");
            foreach (HtmlNode row in node.SelectNodes("tr"))
            {
                if (row.InnerText != "" && row.InnerHtml.IndexOf("<th>") < 0 && row.InnerHtml.IndexOf("background-color") < 0)
                {
                    TableRow tRow = new TableRow();                   
                    foreach (HtmlNode cell in row.SelectNodes("td"))
                    {
                        TableCell tCell = new TableCell();
                        tCell.Text = cell.InnerText;
                        tRow.Cells.Add(tCell);
                    }
                    NLTable.Rows.Add(tRow);
                }
            }
            rgSynchronize.DataSource = NLTable;
            rgSynchronize.DataBind();
        }

 

Cell Sample code:

WebClient webClient = new WebClient();
            //string page = webClient.DownloadString("http://nolimits.art.pl/grafik/wyswietl.php?typ=1");
            StreamReader page = new StreamReader(WebRequest.Create("http://nolimits.art.pl/grafik/wyswietl.php?typ=2").GetResponse().GetResponseStream(), Encoding.UTF8);

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(page);

            HtmlNode node = doc.DocumentNode.SelectSingleNode("//table");
            string context = node.InnerHtml;
            DataTable NLTable = new DataTable();
            NLTable.Columns.Add(" ");//CheckBox
            NLTable.Columns.Add("Dzien tygodnia");
            NLTable.Columns.Add("Godziny zajec");
            NLTable.Columns.Add("Nazwa zajec");
            NLTable.Columns.Add("Poziom");
            NLTable.Columns.Add("Instruktor");
            //NLTable.Columns.Add("Nr sali");
            NLTable.Columns.Add("Wolne miejsce dla");
            foreach (HtmlNode row in node.SelectNodes("tr"))
            {
                if (row.InnerText != "" && row.InnerHtml.IndexOf("<th>") < 0 && row.InnerHtml.IndexOf("background-color") < 0)
                    NLTable.Rows.Add(row.InnerHtml);
            }
            rgSynchronize.DataSource = NLTable;

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 10 Sep 2015, 08:00 AM
Hello,

The problem may be caused by the way the grid is bound. From the code I see that simple data-binding is used(calling .DataBind()). Please modify the grid code so that advanced data-binding is used and test the page again. If this does not prove helpful please share with us the entire page contents(markup and code-behind) so we could further research the matter.

Regards,
Angel Petrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Jakub
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or