Click here to Skip to main content
15,878,708 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Richard Deeming1-Nov-23 5:47
mveRichard Deeming1-Nov-23 5:47 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 8:51
Fokwa Divine1-Nov-23 8:51 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
jschell2-Nov-23 4:30
jschell2-Nov-23 4:30 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine3-Nov-23 6:34
Fokwa Divine3-Nov-23 6:34 
Questionc Pin
Gorla Bhagyasree31-Oct-23 23:19
Gorla Bhagyasree31-Oct-23 23:19 
AnswerRe: c Pin
Richard Deeming31-Oct-23 23:56
mveRichard Deeming31-Oct-23 23:56 
AnswerRe: c Pin
Pete O'Hanlon1-Nov-23 21:26
mvePete O'Hanlon1-Nov-23 21:26 
QuestionC# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown26-Oct-23 11:15
Tridip Bhattacharjee from Unknown26-Oct-23 11:15 
I have a winform project where i am using web browser control which load a site. the site has many tabular data which is a html table. user will select large text from web browser control using their mouse. the select may have many data including multiple tabular data which is nothing but a html table.

I know how to get text from selection. this is sample code which return text.

private string GetSelectedText()
{
    dynamic document = webBrowser1.Document.DomDocument;
    dynamic selection = document.selection;
    dynamic text = selection.createRange().text;
    return (string)text;
}


But i need html of selected area on web browser control programmatically. i use a code sample which suppose to return html of selected portion of web page loaded into web browser control.....but no luck.

here i am sharing that code which not working as expected. please see my code and tell me how could grab the html content from web browser control of large selection ?

here is the code which is not working.

C#
<pre>using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HtmlTableParser
{
    public partial class Form2 : Form
    {
        private WebBrowser webBrowser1;
        public Form2()
        {
            InitializeComponent();

            Button btn = new Button();
            btn.Text = "Test";
            btn.Click += button1_Click;
            this.Controls.Add(btn);

            var panel = new Panel();
            panel.Top = btn.Height + 2;
            panel.Height = this.ClientSize.Height - btn.Height + 2;
            panel.Width = this.ClientSize.Width;
            panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;

            webBrowser1 = new WebBrowser();
            webBrowser1.Dock = DockStyle.Fill;
            webBrowser1.Url = new Uri("https://www.sec.gov/Archives/edgar/data/1108134/000110813423000018/bhlb-20230630.htm");

            panel.Controls.Add(webBrowser1);
            this.Controls.Add(panel);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestSelection();
            TestAllTable();
        }

        private void TestSelection()
        {
            var domdoc = this.webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
            var sel = domdoc.selection;
            var range = sel.createRange();
            var trange = range as mshtml.IHTMLTxtRange;

            var table = GetParentTable(trange.parentElement());
            if (table == null)
            {
                var startPointRange = trange.duplicate();
                startPointRange.setEndPoint("EndToStart", trange);
                var startPointTable = GetParentTable(startPointRange.parentElement());

                var endPointRange = trange.duplicate();
                startPointRange.setEndPoint("StartToEnd", trange);
                var endPointTable = GetParentTable(endPointRange.parentElement());

                if (startPointTable != null)
                {
                    table = startPointTable;
                }
                else if (endPointTable != null)
                {
                    table = endPointTable;
                }
                else
                {
                    MessageBox.Show("Selection is not in Table");
                    return;
                }
            }

            var tableData = TableData.GetTableData(table);

            System.Diagnostics.Debug.WriteLine(tableData.ToString());
        }

        private mshtml.IHTMLTable GetParentTable(mshtml.IHTMLElement element)
        {
            var parent = element;
            while (parent != null)
            {
                if (parent is mshtml.IHTMLTable table)
                {
                    return table;
                }
                parent = parent.parentElement;
            }
            return null;
        }

        private void TestAllTable()
        {
            var domdoc = this.webBrowser1.Document.DomDocument as mshtml.HTMLDocument;
            foreach (var table in domdoc.getElementsByTagName("table").OfType<mshtml.IHTMLTable>())
            {

                var tableData = TableData.GetTableData(table);

                System.Diagnostics.Debug.WriteLine(tableData.ToString());
                System.Diagnostics.Debug.WriteLine(new string('=', 20));
            }
        }

    }



    class TableData
    {
        public static TableData GetTableData(mshtml.IHTMLTable table)
        {
            TableData tableData = new TableData();

            foreach (var tableRow in table.rows.OfType<mshtml.IHTMLTableRow>())
            {
                RowData rowdata = new RowData();
                foreach (var tablecell in tableRow.cells.OfType<mshtml.HTMLTableCell>())
                {
                    CellData cell = new CellData();
                    cell.Text = tablecell.innerText;
                    cell.RowSpan = tablecell.rowSpan;
                    cell.ColSpan = tablecell.colSpan;
                    rowdata.Add(cell);
                }

                tableData.Rows.Add(rowdata);
            }

            return tableData;
        }

        public List<RowData> Rows { get; } = new List<RowData>();


        public override string ToString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (var row in this.Rows)
            {
                sb.AppendLine(row.ToString());
            }
            return sb.ToString();
        }
    }

    class RowData : List<CellData>
    {
        public override string ToString()
        {
            return string.Join("\t", this.Select(cell => cell.Text + new string('\t', cell.ColSpan)));
        }
    }

    class CellData
    {
        public string Text { get; set; }
        public int ColSpan { get; set; }
        public int RowSpan { get; set; }

        public override string ToString() => Text;
    }
}


Here i am pasting a image which show how user will selected the portion of page.
screen shot

It is my request that for last few days i have tried many approach to get html of selection portion from web browser control....but not succeeded. please some one help me with right approach.

Thanks

SuggestionRe: C# How to grab html from selection of web browser control Pin
Richard Deeming26-Oct-23 21:47
mveRichard Deeming26-Oct-23 21:47 
GeneralRe: C# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown27-Oct-23 3:38
Tridip Bhattacharjee from Unknown27-Oct-23 3:38 
GeneralRe: C# How to grab html from selection of web browser control Pin
Richard Deeming27-Oct-23 3:51
mveRichard Deeming27-Oct-23 3:51 
QuestionADP Enterprise API help Pin
Mike Olmon 202125-Oct-23 12:10
Mike Olmon 202125-Oct-23 12:10 
AnswerRe: ADP Enterprise API help Pin
Richard Deeming25-Oct-23 22:32
mveRichard Deeming25-Oct-23 22:32 
GeneralRe: ADP Enterprise API help Pin
jschell26-Oct-23 4:58
jschell26-Oct-23 4:58 
QuestionIs there a way to Convert binding dataMember? Pin
Geanny Martin24-Oct-23 2:50
Geanny Martin24-Oct-23 2:50 
AnswerRe: Is there a way to Convert binding dataMember? Pin
Alan N24-Oct-23 4:21
Alan N24-Oct-23 4:21 
NewsEvergrowth.AspForMarkDigExtension is out there now... Pin
James McCullough19-Oct-23 15:25
professionalJames McCullough19-Oct-23 15:25 
GeneralRe: Evergrowth.AspForMarkDigExtension is out there now... Pin
OriginalGriff19-Oct-23 18:22
mveOriginalGriff19-Oct-23 18:22 
GeneralRe: Evergrowth.AspForMarkDigExtension is out there now... Pin
James McCullough19-Oct-23 18:42
professionalJames McCullough19-Oct-23 18:42 
GeneralRe: Evergrowth.AspForMarkDigExtension is out there now... Pin
OriginalGriff19-Oct-23 20:39
mveOriginalGriff19-Oct-23 20:39 
GeneralRe: Evergrowth.AspForMarkDigExtension is out there now... Pin
jschell20-Oct-23 11:28
jschell20-Oct-23 11:28 
QuestionC# How to iterate in all TR & TD of multiple html table and extract value Pin
Tridip Bhattacharjee from Unknown18-Oct-23 23:11
Tridip Bhattacharjee from Unknown18-Oct-23 23:11 
AnswerRe: C# How to iterate in all TR & TD of multiple html table and extract value Pin
Richard MacCutchan19-Oct-23 4:11
mveRichard MacCutchan19-Oct-23 4:11 
GeneralRe: C# How to iterate in all TR & TD of multiple html table and extract value Pin
Dave Kreskowiak19-Oct-23 5:19
mveDave Kreskowiak19-Oct-23 5:19 
GeneralRe: C# How to iterate in all TR & TD of multiple html table and extract value Pin
Richard MacCutchan19-Oct-23 5:24
mveRichard MacCutchan19-Oct-23 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.