2012-02-20 14 views
3

私は見つけることができる唯一のソリューションを使用せずに文字列またはHTMLファイルにC#のHTMLDocumentを変換します使用したWebブラウザーやHAP

  mshtml.HTMLDocument htmldocu = new mshtml.HTMLDocument(); 
      htmldocu .createDocumentFromUrl(url, ""); 

を、私はパフォーマンスについてわからない、それがHTMLをロードするよりも良いはずですWebBrowserでファイルを開き、そこからHtmlDocumentを取得します。とにかく、そのコードは私のマシンでは動作しません。 2行目を実行しようとすると、アプリケーションがクラッシュします。

これを効率的に行う方法や他の方法はありますか?

注:DOM処理には、HtmlDocumentオブジェクトが必要です。私はhtml文字列は必要ありません。

+0

これに関する解決策を見つけましたか? –

答えて

1

WebClientオブジェクトのDownloadStringメソッドを使用します。例えば上記の例で

WebClient client = new WebClient(); 
string reply = client.DownloadString("http://www.google.com"); 

実行後、replyエンドポイントhttp://www.google.comのHTMLマークアップを含むであろう。 (私はこの回答を投稿時) 4年前から、あなたの実際の質問に答えるための試みで

WebClient.DownloadString MSDN

+0

アイデアは、HTMLの文字列ではなく、DOM解析用のHtmlDocumentオブジェクトを取得しています。WebclientはHtmlDocumentではなくhtml文字列を返します。 – Devela

0

、私は実用的なソリューションを提供しています。あなたがこれを行う別の方法を見つけたら、私は驚くことはありません。だから、これはほとんど同じような解決策を探している他の人たちのためのものです。 (推奨されるソリューションは、HtmlAgilityPackまたはCsQueryまたはいくつかの他のを使用することであるパー​​スHTML DOMを処理するための最良の方法ではない

  • HtmlDocumentの実際の使用)、やや時代遅れこれは

    1. と考えていること、しかし、覚えておいてください実際の解析ではなく、正規表現を用いた方法)
    2. 極めてそれを
    3. を行うための最も安全な/最も互換性のある方法ハックので、ないあなたが本当には私が
    4. 0を表示しようとしてよ何やってはいけません

    また、HtmlDocumentが本当にmshtml.HTMLDocument2ための単なるラッパーなので、それはだけで直接COMラッパーを使用するよりも技術的に遅いですが、私は完全に単にコーディングを容易にするためのユースケースを理解していることに注意してください。

    あなたが上記のすべてでうまくいけば、あなたが望むものを達成する方法はここにあります。

    public class HtmlDocumentFactory 
    { 
        private static Type htmlDocType = typeof(System.Windows.Forms.HtmlDocument); 
        private static Type htmlShimManagerType = null; 
        private static object htmlShimSingleton = null; 
        private static ConstructorInfo docCtor = null; 
    
        public static HtmlDocument Create() 
        { 
        if (htmlShimManagerType == null) 
        { 
         // get a type reference to HtmlShimManager 
         htmlShimManagerType = htmlDocType.Assembly.GetType(
         "System.Windows.Forms.HtmlShimManager" 
         ); 
         // locate the necessary private constructor for HtmlShimManager 
         var shimCtor = htmlShimManagerType.GetConstructor(
         BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null 
         ); 
         // create a new HtmlShimManager object and keep it for the rest of the 
         // assembly instance 
         htmlShimSingleton = shimCtor.Invoke(null); 
        } 
    
        if (docCtor == null) 
        { 
         // get the only constructor for HtmlDocument (which is marked as private) 
         docCtor = htmlDocType.GetConstructors(
         BindingFlags.NonPublic | BindingFlags.Instance 
         )[0]; 
        } 
    
        // create an instance of mshtml.HTMLDocument2 (in the form of 
        // IHTMLDocument2 using HTMLDocument2's class ID) 
        object htmlDoc2Inst = Activator.CreateInstance(Type.GetTypeFromCLSID(
         new Guid("25336920-03F9-11CF-8FD0-00AA00686F13") 
        )); 
        var argValues = new object[] { htmlShimSingleton, htmlDoc2Inst }; 
        // create a new HtmlDocument without involving WebBrowser 
        return (HtmlDocument)docCtor.Invoke(argValues); 
        } 
    } 
    

    それを使用するには、次の

    var htmlDoc = HtmlDocumentFactory.Create(); 
    htmlDoc.Write("<html><body><div>Hello, world!</body></div></html>"); 
    Console.WriteLine(htmlDoc.Body.InnerText); 
    // output: 
    // Hello, world! 
    

    私が直接このコードをテストしていない - 私はあなたが要求している同じ機能を必要な古いPowerShellスクリプトからそれを翻訳しています。失敗した場合は、教えてください。機能はありますが、作業を開始するにはコードを微調整する必要があります。

  • 関連する問題