在mvc中如何获取html页面,如何在MVC中获取网页的特定部分?

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值