简体   繁体   中英

HtmlAgilityPack - How to get the Value from the tag <div> into aspx.cs

I have a task to do. I need to get the value from the tag into c#. I need to display the value into Label This here is my HTML code:

<div size="10" id="para1"></div>  <p></p><asp:Label ID="Label2" runat="server" Text="" Font-Size="XX-Large"></asp:Label><p></p><asp:Label ID="Label3" runat="server" Text="" Font-Size="XX-Large"></asp:Label>

<script>
document.getElementById("para1").innerHTML = formatAMPM();

    function formatAMPM() {
        var d = new Date(),

            days = ['sonday', 'monday', 'tuesday', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
        return days[d.getDay()];
    }</script>

from code behind i use this here to get the value but it does not work:

HtmlDocument page = new HtmlWeb().Load(@"D:\Downloads\wichtig\try\WebApplication1-Kopie-Kopie\MeineWebseite\löschen\WebForm2.aspx");
        var title = page.DocumentNode.SelectSingleNode("//div[@id='para1']");
        Label3.Text = title.ToString();

I only know how to get the tag by Id. This is how it works

 document2.Load(@"D:\Downloads\wichtig\try\WebApplication1-Kopie-Kopie\MeineWebseite\löschen\WebForm2.aspx");

        string tag = document2.GetElementbyId("para1").Name;
        Label2.Text = tag;

That is how it works but I want Label3 should display the current date like

<div>id="para1</div>

it does.This here is the result enter image description here

You should use InnerText property:

var title = page.DocumentNode.SelectSingleNode("//div[@id='para1']");
Label3.Text = title.InnerText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM