2016-12-11 2 views
-1

以下のHTMLテキストがSQL Serverから取得されています。HTMLをプレーンテキストに変換できません:ASP.net

"<span style="color: rgb(36, 39, 41); font-family: Arial, &quot;Helvetica Neue&quot;, Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>" 

htmlをプレーンテキストに変換し、asp.netのラベルに設定したいとします。私は、データセット内のSQLサーバーからデータを取得している

  1. :これを行うには

    。後ろのコードで

     <h3 id="lblqNotes" runat="server" class="text-questiondata" style="color:#1E90FF"> 
        </h3> 
    

にラベルを付けるために取得した上で設定し

  • ラベルに

    lblqNotes.InnerText = System.Net.WebUtility.HtmlDecode(values[2]); 
    

    しかし、出力はまだHTMLコードではなく、プレーンテキストです。

    ありがとうございました。

  • +0

    あなたはHtmlEncodeを意味しますか? https://msdn.microsoft.com/en-us/library/w3te6wfz(v=vs.110).aspx – TigOldBitties

    +0

    HtmlEncodeはPlainTextに変換するためのものではありません。それは...ですか? –

    +0

    あなたはそれが何を読んでみませんか、友人ですか? – TigOldBitties

    答えて

    1

    あなたはXmlDocumentを使用することによって、これを達成することができます:

    static void Main(string[] args) 
        { 
         var xmlDocument = new XmlDocument(); 
         var html = @"<span style=""color: rgb(36, 39, 41); font - family: Arial, &quot; Helvetica Neue&quot;, Helvetica, sans - serif; font - size: 15px; background - color: rgb(255, 255, 255); "">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>"; 
         xmlDocument.LoadXml(html); 
    
         var text = xmlDocument.InnerText; 
         // So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output. 
        } 
    
    +0

    Amazing。最も簡単なことです。単なる質問です。xmlDocument.LoadXml(@)のようなLoadXmLメソッドでhtmlコードを持つ変数に "@"を使用するにはどうすればいいですか? –

    +0

    @はちょうど逐語的な文字列ですが、その変数 –

    +0

    を引き出す質問を更新しましたが、私の場合はdbからデータを引き出し、変数にデータを格納しています。だから私はそのvaribale @をどのように置くのですか?私はこの文字列を試しましたvar = @values [2];それはエラーを投げます。ここで値[2]はhtmlコードを持っています。 –

    0

    は、正規表現を使用してHTMLを取り除くためにこのコードを試してみてください。output = Regex.Replace(source, "<[^>]*>", string.Empty);

    +0

    これは私にこの美しさを考えさせるhttp://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – TigOldBitties

    +0

    これはhtml文字をストリッピングするためのものです。私がする必要があるのはプレーンテキストに変換することです。例:HTML hello。出力は太字で表示されます。 –

    +0

    あなたが必要とするのはHtmlパーサーがこの投稿をチェックすることです。http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c –

    関連する問題