簡體   English   中英

在C#中讀取文本文件

[英]text file reading in c#

我有一個文本文件,其內容來自郵件正文。它包含html代碼。

我只想從該文本文件中獲取href標記。我想使用asp.net c#Web應用程序執行此操作。

是否有人有代碼可以幫助我...

謝謝

嘗試使用HTML Agility Pack來解析電子郵件中的HTML,並從<a>標簽提取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]);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM