2011-05-19 36 views
45

Estoy tratando de analizar el siguiente archivo HTML, me gustaría obtener el valor de la clave. Esto se está haciendo en el teléfono Silverlight para Windows.Analizando HTML con C# .net

<HTML> 
<link ref="shortcut icon" href="favicon.ico"> 
<BODY> 
<script Language="JavaScript"> 
location.href="login.html?key=UEFu1EIsgGTgAV7guTRhsgrTQU28TImSZkYhPMLj7BChpBkvlCO11aJU2Alj4jc5" 
</script> 
<CENTER><a href="login.html?key=UEFu1EIsgGTgAV7guTRhsgrTQU28TImSZkYhPMLj7BChpBkvlCO11aJU2Alj4jc5">Welcome</a></CENTER></BODY></HTML> 

¿Alguna idea de dónde ir desde aquí?

gracias

+1

Acabo de agregar una pregunta a [Recomendaciones de software] (http://softwarerecs.stackexchange.com/) Sitio de pila de intercambio para esto - [Biblioteca C# para analizar HTML? - Recomendaciones de software Stack Exchange] (http://softwarerecs.stackexchange.com/questions/10773/c-library-for-parsing-html/10774#10774). –

Respuesta

66

Dele un vistazo al HTMLAgilityPack. Es un analizador de HTML bastante decente

http://html-agility-pack.net/?z=codeplex

======

Aquí hay un código para que pueda empezar (requiere la comprobación de errores)

HtmlDocument document = new HtmlDocument(); 
string htmlString = "<html>blabla</html>"; 
document.LoadHtml(htmlString); 
HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//a"); 
foreach (HtmlNode link in collection) 
{ 
    string target = link.Attributes["href"].Value; 
} 
+1

+1 He usado esta herramienta antes y es genial. – pixelbobby

+0

Hacemos muchos rasguños usando el paquete Agility y se balancea. Definitivamente prueba esto. – Pat

+1

No creo que puedas usar el paquete de agilidad para el teléfono con Windows. – Nathan

0

Puede utilizar expresiones regulares (Regex class) para ello. La expresión puede ser algo así: login.html\?key=[^"]*

+4

No declinaré nada porque soy amable, pero RegEx ya no es una forma segura de hacer esto, más bien HTMLAgilityPack es casi un estándar de oro en estos días. – pixelbobby

+11

-1 (desafortunadamente, soy justo, nada que ver con * nice *) y esta información también te ayudará a no tratar de analizar HTML utilizando RexEx) http://stackoverflow.com/questions/1732348/ regex-match-open-tags-except-xhtml-self-contained-tags/1732454 # 1732454 –

+2

Regex puede funcionar, pero sugiero lo contrario, para el futuro. – Pat