2012-12-04 34 views
0

我有一個Outlook加載項,它將把MailItem保存其附件和html內容到可作爲網頁查看的位置。問題是,Outlook爲每個附件添加了兩組十六進制代碼,這裏是一個示例。清理由Outlook生成的img src

<img width=700 height=119 id="_x0000_i1032" src="http://somesite/img/didyouknow/[email protected]" alt="diduknow_header.gif"> 

從上面爲所有圖像移除01CD34FA.041E5EE0的最簡潔方法是什麼?

回答

0

簡單: 既然你得到從前景負載完整的XML文檔首先到XmlDocument

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml(html); 
string imgsrc = xmlDoc["img"].Attributes["src"].InnerText; //I'm just guessing here without the full XML 

imgsrc = imgsrc.Substring(0, imgsrc.LastIndexOf('@')); 

可能想要做錯誤檢查,因爲這將引發異常,如果沒有@符號在串。

+0

我希望能處理整個html文檔的東西。 – Marshall

+0

您可以將html加載到XmlDocument中,並通過以下方式輕鬆找到它: string imgsrc = xmlDoc [「img」]。Attributes [「src」]。InnerText; – Mataniko

+0

我想過,但它似乎非常脆弱,我應該相信Outlook /用戶發送這些電子郵件生成足以走這條路線的HTML有效嗎? – Marshall

0

嘗試搜索了此模式:

(src\=\".*?\.jpg)([^\"]+)(\") 

而且隨着

$1$3 

在代碼替換它將會是:

string input = File.ReadAllText("path/to/the/outlook.mess"); 
string pattern = @"(src\=\"".*?\.jpg)([^\""]+)(\"")"; 
string cleanOutput = Regex.Replace(input, pattern, "$1$3"); 
File.WriteAllText("/path/to/the/outlook.clean", cleanOutput); 

注意,它的重複雙引號兩次需要在一個被引用的字符串中,具有單引號的效果。

+0

@Marshal我更新了我的答案,添加了示例代碼。 –