0

I have a requirement for sending the mail to Client with specific section say div of an aspx page. I have created a sample method which executes the page on server and returns the HTML markup in a StringBuilder object. This works fine. Now, Just i want to get the HTML of Specific Div Content and not the Whole returned page content.

Code:

    protected void ReadPage()
        {
            //StreamReader reader = new StreamReader(Server.MapPath("CompleteOrder.aspx"));
            //string readFile = reader.ReadToEnd();

            ////*** somewhere in an aspx page - preferably a different one to the one you are emailing but doesnt have to be******
            StringWriter pageContents = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(pageContents);
            Server.Execute("CompleteOrder.aspx", htw);
            MailBiz ObjMailBiz = new MailBiz();
            string Username = Session["UserName"].ToString();
            string email = Session["EmailID"].ToString();
            string strSubject = "Test Mail";
            bool issent = ObjMailBiz.SendEmail(Username, email, strSubject, pageContents.ToString());
            if (issent)
            {
                //mail sent successfully
            }
            else
            {
                //failed to sent the mail


      }
}

Just i want to know how to read specific Div content from this returned string ? Eg: Read the div with id="content" (<div id='content'>some other innerdivs tables etc</div>)

Help Appreciated!

3
  • 1
    Have a look into the HtmlAgilityPack. Commented Dec 16, 2013 at 11:25
  • Sorry!Dont want to use any external APIs...! :( Commented Dec 16, 2013 at 11:33
  • 1
    Why not - why spend lots of time doing it roughly when someone has done it properly for you for free! (Note this is a widely used library - has over .5 million downloads on NuGet). If not then probably just string parsing of the stringwriter will have to do (eg you know what your div start tag looks like) Commented Dec 16, 2013 at 11:44

3 Answers 3

3

You can use HtmlAgilityPack to load the HTML and search for the relevant div like this:

StringWriter pageContents = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(pageContents);
Server.Execute("CompleteOrder.aspx", htw);

HtmlDocument html = new HtmlDocument();
html.LoadHtml(pageContents.ToString());

HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@id='content']");

...Here you can access node.InnerText for your message
Sign up to request clarification or add additional context in comments.

1 Comment

You might want the InnerHtml property, to preserve markup ("inner divs, tables etc")
0

Put runat server attribute in Div. so it will be available in c# code.

For your example use

content.innerHTML

or

content.InnerText

to get div contents

Thanks

2 Comments

No! your are not getting it! what i mean is I am executing the page on server and returning the HTML Markup and from that markup read specific div contents. NOT THE DIV content from Code Behind..!
sorry for not getting your point. but now i think in that case you should use xml parser assuming HTML string as xml. i guess it will give well formed value.
0

you can use user control instead of page and then load control in page element then findcontrol like this

Page pa = new Page();
pa.LoadControl("CompleteOrder.ascx");
pa.FindControl("content");

1 Comment

The OP is after the rendered output, not the server side controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.