質問
テキストファイルがあり、そのメール本文のコンテンツにはhtmlコードが含まれています。
そのテキストファイルからhrefタグのみを取得したい。asp.netC#Webアプリケーションでこれを実行したい。
誰かが私を助けるコードを持っていますか...
ありがとう
解決
Html Agility Pack を使用して、メールからHTMLを解析し、 <!> lt; a <!> gt;のhref 属性タグ。
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;
}
他のヒント
完全なソリューションではない場合でも、正規表現を使用できます。
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]);
}
}
}
所属していません StackOverflow