2011-02-02 26 views
28

¿Cómo puedo eliminar etiquetas HTML de la siguiente cadena?Eliminar etiquetas HTML en String

<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/> 

Lo estoy escribiendo a Asponse.PDF, pero las etiquetas HTML se muestran en el PDF. ¿Cómo puedo eliminarlos?

+0

he intentado HTMLDecode, no funcionó – jvm

+0

Es necesario codificar HTML para escapar de las etiquetas. – Joe

+0

¿Desea quitar las etiquetas o aplicar el formato? – SLaks

Respuesta

89

Advertencia: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 No debe usar una expresión regular para analizar una gramática libre de contexto como HTML. Si el HTML lo proporciona una entidad externa, puede ser fácilmente manipulado para evadir su expresión regular. –

+6

'cadena estática pública StripTagsCharArray (fuente de cadena) { \t char [] array = new char [source.Length]; \t int arrayIndex = 0; \t bool inside = false; \t for (int i = 0; i ') \t { \t \t interior = false; \t \t continuar; \t} \t si \t { \t \t array [arrayIndex] = sea (dentro!); \t \t arrayIndex ++; \t} \t} \t return new string (array, 0, arrayIndex); } 'Es aproximadamente 8 veces más rápido que Regex – AuthorProxy

+0

@mehaase En general, estoy de acuerdo. Pero, ¿quién dijo algo sobre el análisis? Él simplemente quiere eliminar las etiquetas. Siempre se debe hacer una distinción fundamental entre realmente PARSING html con expresión regular frente a SEARCHING o MATCHING algún html con regex. – capdragon

10

Se debe utilizar la HTML Agility Pack:

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

Realmente no veo por qué las personas dan la respuesta para usar el Agility Pack, ya que .InnerText del cuerpo (como ejemplo) no representa una cadena libre de marcas. Hay muchas personas en SO que obtienen el Agility Pack y luego se preguntan por qué todavía están mirando etiquetas de script. – radpin