html更改节点但不希望更改原节点,使用HtmlAgilityPack解析节点的子节点的问题

我在解析html中的输入标签子窗体时出现问题。我可以使用// input [@type]从根目录解析它们,但不能作为特定节点的子节点。使用HtmlAgilityPack解析节点的子节点的问题

下面是一些代码,说明了这个问题:

private const string HTML_CONTENT =

"" +

"

" +

"

Test Page" +

"" +

"" +

"

" +

"

" +

"" +

"" +

"

" +

"Someplace" +

"Someplace Image" +

"

" +

"" +

"

" +

"" +

"";

public void Parser_Test()

{

var htmlDoc = new HtmlDocument

{

OptionFixNestedTags = true,

OptionUseIdAttribute = true,

OptionAutoCloseOnEnd = true,

OptionAddDebuggingAttributes = true

};

byte[] byteArray = Encoding.UTF8.GetBytes(HTML_CONTENT);

var stream = new MemoryStream(byteArray);

htmlDoc.Load(stream, Encoding.UTF8, true);

var nodeCollection = htmlDoc.DocumentNode.SelectNodes("//form");

if (nodeCollection != null && nodeCollection.Count > 0)

{

foreach (var form in nodeCollection)

{

var id = form.GetAttributeValue("id", string.Empty);

if (!form.HasChildNodes)

Debug.WriteLine(string.Format("Form {0} has no children", id));

var childCollection = form.SelectNodes("input[@type]");

if (childCollection != null && childCollection.Count > 0)

{

Debug.WriteLine("Got some child nodes");

}

else

{

Debug.WriteLine("Unable to find input nodes as children of Form");

}

}

var inputNodes = htmlDoc.DocumentNode.SelectNodes("//input");

if (inputNodes != null && inputNodes.Count > 0)

{

Debug.WriteLine(string.Format("Found {0} input nodes when parsed from root", inputNodes.Count));

}

}

else

{

Debug.WriteLine("Found no forms");

}

}

什么是输出:

Form form1 has no children

Unable to find input nodes as children of Form

Form form2 has no children

Unable to find input nodes as children of Form

Found 3 input nodes when parsed from root

我会想到的是,Form 1和Form既能有孩子和输入[@type ]将能够找到2个节点的form1和1的form2

是否有一个特定的配置设置或方法,我没有使用,我应该是?有任何想法吗?

感谢,

史蒂夫

2010-06-23

SteveG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值