我需要从互联网上加载一个长字符串,我已经完成了.现在我需要找到H1标头标签并打印内容.
最短或最简单的方法是什么?
for (int x = 0; x < tempString.Length; x++)
{
if (write == 2)
{
name =name + tempString[x];
lenght++;
}
if (tempString[x] == '<' && tempString[x] == 'h' && tempString[x] == '1' )
write = 1;
if (write == 1 && tempString[x] == '>')
write = 2;
if (tempString[x] == '-' && write == 1)
write = 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道这有点奇怪.但这就是我的全部.
使用HTML Agility Pack - 您尝试的其他任何事情都会让您头疼.
HtmlAgility示例:
var html = "<html><head></head><body><h1>hello</h1></body></html>";
HtmlDocument d = new HtmlDocument();
d.LoadHtml(html);
var h1Contents = d.DocumentNode.SelectSingleNode("//h1").InnerText;
Run Code Online (Sandbox Code Playgroud)