【发布时间】:2023-03-12 12:45:01
【问题描述】:
我想从这个 html-sn-p 中提取两个源属性:
<audio controls>
<source src="horse.mp3" type="audio/mpeg">
<source src="horse.ogg" type="audio/ogg">
<embed height="50" width="100" src="horse.mp3">
</audio>
这是我的工作:
首先,我提取所有音频标签(包括您在上面看到的那个):
var audio_tags = HtmlDoc.DocumentNode.SelectNodes("//" + HTML.TAG_AUDIO);
之后,我尝试使用这段代码从 HtmlNodeCollection audio_tags 中提取源元素:
foreach (HtmlNode link in audio_tags)
{
if (link != null)
{
string url;
string type;
// select all source tags, see here for an example: http://www.w3schools.com/html/html_sounds.asp
if(link.HasChildNodes)
{
var children = link.ChildNodes;
if (children != null)
{
foreach (HtmlNode child in children)
{
Console.WriteLine(children[0].GetAttributeValue("type", "err").ToString() + "||" + children[0].OriginalName);
Console.WriteLine(children[1].GetAttributeValue("type", "errrr").ToString() + "||" + children[1].OriginalName);
...
写行表明第一个元素不存在,因为打印了“err”。但它应该是第一个源元素。我会很高兴有一些提示。
编辑:
这些文字的输出是:
err||#text
audio/mpeg||source
还有nr。子元素的数量是 2。
【问题讨论】:
-
这个“音频控制”元素是什么? XML 中的元素不能有空格。如果“controls”是一个属性,它必须有一个值。
-
你对!音频是一个新的 html5 元素。并且here 进行了解释。总结一下:在XHTML中,属性最小化是被禁止的,controls属性必须定义为
标签: c# html-parsing html-agility-pack