0

htmlのフォームの入力タグの子を解析する際に問題が発生しました。// input [@type]を使用してルートから解析できますが、特定のノードの子として解析することはできません。

問題を説明するコードを次に示します。

private const string HTML_CONTENT =
        "<html>" +
        "<head>" +
        "<title>Test Page</title>" +
        "<link href='site.css' rel='stylesheet' type='text/css' />" +
        "</head>" +
        "<body>" +
        "<form id='form1' method='post' action='http://www.someplace.com/input'>" +
        "<input type='hidden' name='id' value='test' />" +
        "<input type='text' name='something' value='something' />" +
        "</form>" +
        "<a href='http://www.someplace.com'>Someplace</a>" +
        "<a href='http://www.someplace.com/other'><img src='http://www.someplace.com/image.jpg' alt='Someplace Image'/></a>" +
        "<form id='form2' method='post' action='/something/to/do'>" +
        "<input type='text' name='secondForm' value='this should be in the second form' />" +
        "</form>" +
        "</body>" +
        "</html>";

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

私が期待するのは、Form1とForm2の両方に子があり、input[@type]はform1の2つのノードとform2の1つのノードを見つけることができるということです。

使用していない特定の構成設定または方法はありますか?何か案は?

ありがとう、

スティーブ

4

2 に答える 2

4

HtmlAgilityPackサイトのこのディスカッションスレッドをチェックして ください-http ://htmlagilitypack.codeplex.com/workitem/21782

これは彼らが言うことです:

これはバグではありませんが、機能であり、構成可能です。FORMは、実際には元のHTMLの(強力な)機能であったため、多くのHTMLページでフォームが重複していたため、このように扱われます。XMLとXHTMLが存在するようになったので、誰もがオーバーラップはエラーであると想定していますが、そうではありません(HTML 3.2では)。HtmlNode.csファイルを確認し、ElementsFlagsコレクションを変更します(または、必要に応じて実行時に変更します)

HtmlNode.csファイルを変更するには、次の行をコメントアウトします-

ElementsFlags.Add("form", HtmlElementFlag.CanOverlap | HtmlElementFlag.Empty);
于 2010-06-25T19:30:36.037 に答える
2

さて、私は今のところHtmlAgilityPackをあきらめました。すべてを機能させるために、そのライブラリで行うべき作業はまだまだあるようです。この問題を解決するために、ここからSGMLReaderライブラリを使用するようにコードを移動しました:http://developer.mindtouch.com/SgmlReader

このライブラリを使用すると、すべての単体テストが適切に合格し、サンプルコードは期待どおりに機能します。

于 2010-06-23T16:34:18.363 に答える