Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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#解析HTML网页_C#_Wpf - Fatal编程技术网

C#解析HTML网页

C#解析HTML网页,c#,wpf,C#,Wpf,如何用C解析一个完整的HTML网站# 小例子 <html> <head></head> <body> <div class="wrapper"> <div class="row"> <div>Value1</div> <div>Value2</div> </div> <div class="row"> &

如何用C解析一个完整的HTML网站#

小例子

<html>
 <head></head>
 <body>
  <div class="wrapper">
   <div class="row">
    <div>Value1</div>
    <div>Value2</div>
   </div>
   <div class="row">
    <div>Value1</div>
    <div>Value2</div>
   </div>
   <div class="row">
    <div>Value1</div>
    <div>Value2</div>
   </div>
   <div class="row">
    <div>Value1</div>
    <div>Value2</div>
   </div>
  </div>
 </body>
</html>

在断点出现之前,一切正常,但在出现断点之前,我不知道该如何继续。

您可以使用提取数据。它的用法类似于parse libs。对于您的html示例,它类似于:

IEnumerable<WrapperItem> items = WebContentParser.ParseList<WrapperItem>(html);

// ...

[ListSelector(".wrapper", ChildSelector = ".row")]
class WrapperItem
{
    [Selector("div:nth-child(1)")]
    public string Value1 { get; set; }

    [Selector("div:nth-child(2)")]
    public string Value2 { get; set; }
}

取决于HTML HTML的格式不是很好的XML,应该考虑使用解析。
IEnumerable<WrapperItem> items = WebContentParser.ParseList<WrapperItem>(html);

// ...

[ListSelector(".wrapper", ChildSelector = ".row")]
class WrapperItem
{
    [Selector("div:nth-child(1)")]
    public string Value1 { get; set; }

    [Selector("div:nth-child(2)")]
    public string Value2 { get; set; }
}
WebClient client = new WebClient ();
string html = client.DownloadString("https://example.com");