<option> have no child, why?

Topics: User Forum
Dec 14, 2006 at 9:00 PM
Hi.

Here the sample.
Page source
<html>
<head>
<title>test</title>
</head>
<body>
<SELECT NAME="myname">
<OPTION VALUE="1">a</OPTION>
<OPTION VALUE="2">b</OPTION>
<OPTION VALUE="3">c</OPTION>
</SELECT>
</body>
</html>

when i try use this code
HtmlWeb hw = new HtmlWeb();
string url = @"D:\test.htm";//it contain the source above

HtmlDocument doc = new HtmlDocument();
doc = hw.Load(url);

HtmlNodeCollection options = doc.DocumentNode.SelectNodes("//select@name='myname'/option@value!=''");

there
(options0 as HtmlNode).HasChildNodes is false
it must have text node "a". (This way work if we use XmlDocument for such example)
but why? the bug?

By the way construction options0.HasChildNodes does not work (i mean auto complete does not work in VS2005).


Thanks
Michael
Dec 29, 2006 at 9:38 PM
I solve this problem by fixing bug(?)
in the HtmlNode.cs file change line ElementsFlags.Add("option", HtmlElementFlag.Empty | HtmlElementFlag.Closed);

It was ElementsFlags.Add("option", HtmlElementFlag.Empty);
Coordinator
Jan 1, 2007 at 1:40 PM
Hey, it's not a bug, it's a feature :-)
By default, OPTION does not have any child because that's the case in HTML 3.2. And the HTML Agility Pack mostly target HTML 3.2.
You can change it using the public static field ElementFlags (what you found), you don't have to modify the source of the library.
Jan 11, 2007 at 6:34 PM
Ah, sorry for "bug" :)

But seems it need to check if the <option> have closure (Like AutoDetectEncoding working, some sort of html version autodetecting ). Other way this looks like bug :)

Early the Html Agility Pack some slogan was "open doc from inet and save it - no differences will be. no other known tool can do this." - I found this in discussion (with small changes, but idea the same). For now this is not always work :( (with default settings).


But surely - this tool is great.
So in any way - big thanks!

:)