2011-02-02 17 views
28

Come rimuovere i tag HTML dalla stringa seguente?Rimuovi tag HTML in stringa

<P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal><SPAN style="LINE-HEIGHT: 115%; 
FONT-FAMILY: 'Verdana','sans-serif'; COLOR: #333333; FONT-SIZE: 9pt">In an 
email sent just three days before the Deepwater Horizon exploded, the onshore 
<SPAN style="mso-bidi-font-weight: bold"><b>BP</b></SPAN> manager in charge of 
the drilling rig warned his supervisor that last-minute procedural changes were 
creating "chaos". April emails were given to government investigators by <SPAN 
style="mso-bidi-font-weight: bold"><b>BP</b></SPAN> and reviewed by The Wall 
Street Journal and are the most direct evidence yet that workers on the rig 
were unhappy with the numerous changes, and had voiced their concerns to <SPAN 
style="mso-bidi-font-weight: bold"><b>BP</b></SPAN>’s operations managers in 
Houston. This raises further questions about whether <SPAN 
style="mso-bidi-font-weight: bold"><b>BP</b></SPAN> managers properly 
considered the consequences of changes they ordered on the rig, an issue 
investigators say contributed to the disaster.</SPAN></p><br/> 

Sto scrivendo su Asponse.PDF, ma i tag HTML sono visualizzati nel PDF. Come posso rimuoverli?

+0

ho provato HTMLDecode, non ha funzionato – jvm

+0

È necessario codificare HTML per sfuggire ai tag. – Joe

+0

Vuoi rimuovere i tag o applicare la formattazione? – SLaks

risposta

89

Attenzione:This does not work for all cases and should not be used to process untrusted user input.

using System.Text.RegularExpressions; 
... 
const string HTML_TAG_PATTERN = "<.*?>"; 

static string StripHTML (string inputString) 
{ 
    return Regex.Replace 
    (inputString, HTML_TAG_PATTERN, string.Empty); 
} 
+8

-1 Non si dovrebbe usare un'espressione regolare per analizzare una grammatica senza contesto come HTML. Se l'HTML viene fornito da qualche entità esterna, allora può essere facilmente manipolato per eludere la tua espressione regolare. –

+6

'stringa statica pubblica StripTagsCharArray (stringa di origine) { \t char [] array = new char [source.Length]; \t int arrayIndex = 0; \t bool interno = falso; \t per (int i = 0; i ') \t { \t \t all'interno = false; \t \t continuare; \t} \t se \t { \t \t array [arrayIndex] = let (dentro!); \t \t arrayIndex ++; \t} \t} \t return new string (array, 0, arrayIndex); } 'È circa 8 volte più veloce di Regex – AuthorProxy

+0

@mehaase Per la maggior parte sono d'accordo. Ma chi ha detto qualcosa sull'analisi? Vuole semplicemente rimuovere i tag. Una distinzione fondamentale deve sempre essere fatta tra realmente PARSING html con regex contro SEARCHING o MATCHING alcuni html con regex. – capdragon

10

si dovrebbe usare il HTML Agility Pack:

HtmlDocument doc = ... 
string text = doc.DocumentElement.InnerText; 
+17

Io davvero non capisco perché le persone danno la risposta di utilizzare la Agility Pack, dal momento che. Il testo del corpo (ad esempio) non esegue il rendering di una stringa senza markup. Ci sono molte persone su SO che ottengono il Agility Pack e si chiedono perché continuano a guardare markup, tag script. – radpin