0

重複の可能性:
C#でhtmlを解析するための最良の方法は何ですか?

HTMLファイルを解析しています。HTML内のすべてのhrefタグを見つけて、テキスト対応バージョンに置き換える必要があります。

これが例です。

Original Text: <a href="http://foo.bar">click here</a> 
replacement value: click here <http://foo.bar>

どうすればこれを達成できますか?

4

1 に答える 1

4

次のようなコードで、HtmlAgilityPackライブラリを使用できます。

        HtmlDocument doc = new HtmlDocument();
        doc.Load(myHtmlFile); // load your file

        // select recursively all A elements declaring an HREF attribute.
        foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]"))
        {
            node.ParentNode.ReplaceChild(doc.CreateTextNode(node.InnerText + " <" + node.GetAttributeValue("href", null) + ">"), node);
        }

        doc.Save(Console.Out); // output the new doc.
于 2012-10-29T17:06:28.150 に答える