I want to get a specific part of current page to save it in another file for example in a PDF file.
I want to get this part of current view in ActionResult when I click on submit:
.......
I tried to use this code to get :
client = new WebClient();
string url = HttpContext.Request.Url.AbsoluteUri;
string content = "";
Stream data = client.OpenRead(url);
StreamReader sr = new StreamReader(data);
content = sr.ReadToEnd();
data.Flush();
data.Close();
But I want to get only tag div named t1 from current page.
NOTE: I don't want to use jquery. I only want to do it in C#.
Solutions1
try using HTMLAgilityPack for this sort of thing...
example code block;
public string GetContent(string url)
{
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(url);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@id='t1']");
return node.InnerHtml;
}
Talk1:
But an error occurred: An exception of type 'System.NullReferenceException' occurred in YourProject.dll but was not handled in user code
Talk2:
doc.DocumentNode.SelectSingleNode("//div[@id='t1']"); returns null
Talk3:
Yes. I added but It still returns null.
Talk4:
i tried it , its working well ,i guess you are doing something wrong.
Talk5:
I think something works wrong because on another page it works. Thanks for your help!