Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ解析带有HtmlAgilityPack的HTML页面_C#_Linq_Html Agility Pack - Fatal编程技术网

C# 使用LINQ解析带有HtmlAgilityPack的HTML页面

C# 使用LINQ解析带有HtmlAgilityPack的HTML页面,c#,linq,html-agility-pack,C#,Linq,Html Agility Pack,如何在网页上使用Linq解析html并向字符串添加值。我正在metro应用程序上使用HtmlAgilityPack,希望返回3个值并将它们添加到字符串中 以下是url= 我想从以下内容中获取值,请参见“belwo” “余额:”, “中的交易”, “收到” 您必须解析的文档不是用于解析的格式最完善的文档。许多元素缺少类或至少id属性,但您希望得到的是第二个p标记 内容 你可以试试这个 HtmlDocument htmlDocument = new HtmlDocument(); htmlDocum

如何在网页上使用Linq解析html并向字符串添加值。我正在metro应用程序上使用HtmlAgilityPack,希望返回3个值并将它们添加到字符串中

以下是url=

我想从以下内容中获取值,请参见“belwo”

“余额:”, “中的交易”, “收到”


您必须解析的文档不是用于解析的格式最完善的文档。许多元素缺少类或至少id属性,但您希望得到的是第二个p标记 内容

你可以试试这个

HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);



var pNodes = htmlDocument.DocumentNode.SelectNodes("//p")
[1].InnerHtml.ToString().Split(new string[] { "<br />" }, StringSplitOptions.None).Take(3);

 string vl="Balance:"+pNodes[0].Split(':')[1]+"Transactions in"+pNodes[1].Split(':')[1]+"Received"+pNodes[2].Split(':')[1];
HtmlDocument HtmlDocument=new HtmlDocument();
htmlDocument.LoadHtml(html);
var pNodes=htmlDocument.DocumentNode.SelectNodes(“//p”)
[1] .InnerHtml.ToString().Split(新字符串[]{“
”},StringSplitOptions.None); string vl=“Balance:”+pNodes[0]。在“+pNodes[1]中拆分(“:”)[1]+”事务。拆分(“:”)[1]+“收到”+pNodes[2]。拆分(“:”)[1];
请尝试以下操作。你也可以考虑把桌子拉开,因为它比“P”标签中的自由文本好一些。 干杯,亚伦

// download the site content and create a new html document
// NOTE: make this asynchronous etc when considering IO performance
var url = "http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data = new WebClient().DownloadString(url);
var doc = new HtmlDocument();
doc.LoadHtml(data);

// extract the transactions 'h3' title, the node we want is directly before it
var transTitle = 
    (from h3 in doc.DocumentNode.Descendants("h3")
     where h3.InnerText.ToLower() == "transactions"
     select h3).FirstOrDefault();

// tokenise the summary, one line per 'br' element, split each line by the ':' symbol
var summary = transTitle.PreviousSibling.PreviousSibling;
var tokens = 
    (from row in summary.InnerHtml.Replace("<br>", "|").Split('|')
     where !string.IsNullOrEmpty(row.Trim())
     let line = row.Trim().Split(':')
     where line.Length == 2
     select new { name = line[0].Trim(), value = line[1].Trim() });

// using linqpad to debug, the dump command drops the currect variable to the output
tokens.Dump();
//下载网站内容并创建新的html文档
//注意:在考虑IO性能时,将此设置为异步etc
变量url=”http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data=new WebClient().DownloadString(url);
var doc=新的HtmlDocument();
doc.LoadHtml(数据);
//提取事务“h3”标题,我们想要的节点就在它前面
变量transTitle=
(来自doc.DocumentNode.substands(“h3”)中的h3)
其中h3.InnerText.ToLower()=“事务”
选择h3.FirstOrDefault();
//标记摘要,每个“br”元素一行,每行用“:”符号分隔
var summary=transTitle.PreviousSibling.PreviousSibling;
变量标记=
(来自summary.InnerHtml.Replace(“
”,“|”)中的行。拆分(“|”) 其中!string.IsNullOrEmpty(row.Trim()) 让line=row.Trim().Split(“:”) 其中line.Length==2 选择新的{name=line[0].Trim(),value=line[1].Trim()}); //使用linqpad进行调试,dump命令将currect变量放到输出中 tokens.Dump();
“Dump()”是一个LinqPad命令,用于将变量转储到控制台,下面是Dump命令的输出示例:

  • 结余:5 LTC
  • 内地的交易:2宗
  • 收到:5 LTC
  • 交易额:0
  • 已发送:0 LTC

我无法使用SelectNodes它在windows应用商店开发中不受支持,这就是我尝试使用“后代:我没有属性”转储的原因“这可能是因为该应用程序是windows应用商店应用程序。还有其他属性可以使用吗?Dump命令上方的注释解释了我在那里做的事情。我使用LinqPad调试和运行脚本,Dump方法只是将变量放到控制台。我将编辑答案以显示命令的输出,这样您就可以可视化结果。不用担心,只需记住标记答案。此外,我认为@COLD告诉我要比我更好地拆分“br”元素上的字符串,我浪费了一个替换的调用,因为他/她的替换更有效,可读性更强。
// download the site content and create a new html document
// NOTE: make this asynchronous etc when considering IO performance
var url = "http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data = new WebClient().DownloadString(url);
var doc = new HtmlDocument();
doc.LoadHtml(data);

// extract the transactions 'h3' title, the node we want is directly before it
var transTitle = 
    (from h3 in doc.DocumentNode.Descendants("h3")
     where h3.InnerText.ToLower() == "transactions"
     select h3).FirstOrDefault();

// tokenise the summary, one line per 'br' element, split each line by the ':' symbol
var summary = transTitle.PreviousSibling.PreviousSibling;
var tokens = 
    (from row in summary.InnerHtml.Replace("<br>", "|").Split('|')
     where !string.IsNullOrEmpty(row.Trim())
     let line = row.Trim().Split(':')
     where line.Length == 2
     select new { name = line[0].Trim(), value = line[1].Trim() });

// using linqpad to debug, the dump command drops the currect variable to the output
tokens.Dump();