lectura de archivos de texto en c #
-
05-07-2019 - |
Pregunta
Tengo un archivo de texto, es el contenido de un cuerpo de correo. Incluye códigos html.
Quiero tomar solo etiquetas href de ese archivo de texto. Quiero hacer esto con la aplicación web asp.net c #.
¿Alguien tiene un código para ayudarme ...
Gracias
Solución
Intente usar el Html Agility Pack para analizar el HTML de su correo electrónico y extraer el href atributos de < a > etiquetas.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(emailBody);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute att = link.Attributes["href"];
string href = att.Value;
}
Otros consejos
Puede usar la expresión regular aunque no sea una solución perfecta:
class Program
{
static void Main(string[] args)
{
var text = File.ReadAllText(@"d:\test.htm");
Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
foreach(Match match in matches)
{
Console.WriteLine(match.Groups[1]);
}
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow