如何在 C# 中 select 一个包含所有子项的 dl 标签
How to select a dl tag with all child in c#
我有问题 select 所有 dt
和 dd
来自打击 HTML table
<dl class="Grid Grid--multicol Grid--2col:40em Grid--4col:60em">
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Item ID:</dt>
<dd class="u-weightSemibold u-paddingL5px">72547664</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> License:</dt>
<dd class="u-weightSemibold u-paddingL5px">
<a class="u-linkDodgerBlue js-infoLicenseString" href="" target="_blank">Standard</a>
</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Usage:</dt>
<dd class="u-weightSemibold">Commercial<span class="TooltipQuestionMark"></span></dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Required Plugins:</dt>
<dd class="u-weightSemibold u-paddingL5px">None</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Creation Tool:</dt>
<dd class="u-weightSemibold u-paddingL5px">Autodesk</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-isHidden:0-60em"></div>
</dl>
我写这个
i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).FirstOrDefault().SelectNodes(".//div").ToList().ForEach(x =>
{
result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() });
result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() });
});
以上代码只是抓取第一行
Item ID:
| 72547664
列出所有 dt
和 dd
信息有什么问题
你的代码的问题是,当你第一次想要用 class Grid
获取所有 dl
元素时,你只获取了 [=20] 这种类型的第一个=] 元素与方法 FirstOrDefault()
.
i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).SelectNodes(".//div").ToList().ForEach(x =>
{
result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() });
result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() });
});
您必须删除 FirstOrDefault()
才能获取适合您需求的所有元素。
我有问题 select 所有 dt
和 dd
来自打击 HTML table
<dl class="Grid Grid--multicol Grid--2col:40em Grid--4col:60em">
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Item ID:</dt>
<dd class="u-weightSemibold u-paddingL5px">72547664</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> License:</dt>
<dd class="u-weightSemibold u-paddingL5px">
<a class="u-linkDodgerBlue js-infoLicenseString" href="" target="_blank">Standard</a>
</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Usage:</dt>
<dd class="u-weightSemibold">Commercial<span class="TooltipQuestionMark"></span></dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Required Plugins:</dt>
<dd class="u-weightSemibold u-paddingL5px">None</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-flexRow">
<dt> Creation Tool:</dt>
<dd class="u-weightSemibold u-paddingL5px">Autodesk</dd>
</div>
<div class="Grid-cell u-flex u-flexOne u-isHidden:0-60em"></div>
</dl>
我写这个
i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).FirstOrDefault().SelectNodes(".//div").ToList().ForEach(x =>
{
result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() });
result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() });
});
以上代码只是抓取第一行
Item ID:
| 72547664
列出所有 dt
和 dd
信息有什么问题
你的代码的问题是,当你第一次想要用 class Grid
获取所有 dl
元素时,你只获取了 [=20] 这种类型的第一个=] 元素与方法 FirstOrDefault()
.
i = 0; doc.DocumentNode.SelectNodes("//dl").Where(x => x.HasClass("Grid")).SelectNodes(".//div").ToList().ForEach(x =>
{
result.Items.Add(new DownloadItem { Name = "specs-title", Index = i, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dt").FirstOrDefault().GetText() });
result.Items.Add(new DownloadItem { Name = "specs", Index = i++, Type = DownloadItemType.Text, Value = x.SelectNodes(".//dd").GetText() });
});
您必须删除 FirstOrDefault()
才能获取适合您需求的所有元素。