Search code examples
c#javascripthtmlinternet-explorerbho

How to use C# read the value of the TD?


This is our CMIS(Computer Management Information System) Server page: (Note: The CMIS server I cannot modify, the page is automatic generation.)

<form method="POST" action="bl_view_invoice_controler.jsp" name="fm1" onSubmit="return checkValid()">
    //please attention the form name is 'fm1'
    <TABLE id=AutoNumber7 style="BORDER-COLLAPSE: collapse" 
        borderColor=#111111 cellSpacing=0 cellPadding=2 width="100%" 
        border=0>
        <TBODY>
            <TR>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
            </TR>
            <TR>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
            </TR>
        </TBODY>
    </TABLE>
    <script language="JavaScript"> //this script store the value of the table
        fm1.ivcd_item['0'].value="B747-400";
        fm1.ivcd_totalfc['0'].value="500.00";
        fm1.ivcd_unitprice['0'].value="1";
        fm1.ivcd_quantity['0'].value="";

        fm1.ivcd_item['2'].value="B747-800";
        fm1.ivcd_totalfc['2'].value="250.00";
        fm1.ivcd_unitprice['2'].value="";
        fm1.ivcd_quantity['2'].value="";
    </script>           
</FORM>

My question is, How to use C# get the value of the table, suach as 'B747-400' or 'B747-800'?


Solution

  • I would also say "use HtmlAgilityPack", if there were no scripts to be executed after the page is downloaded. So My solution is: first downloading the page using WebClient then parsing it using the WebBrowser component.

    WebBrowser web = new WebBrowser();
    web.DocumentCompleted += (sender, args) =>
        {
            var result = web.Document
                .GetElementsByTagName("input")
                .Cast<HtmlElement>()
                .Where(e => e.GetAttribute("name") == "ivcd_item")
                .Select(e=>e.GetAttribute("value"))
                .ToArray();
        };
    web.DocumentText = htmlstring;