0
我在解析outlook电子邮件正文消息时有点困惑。发件人似乎以HTML格式发送消息,但是当我尝试使用HTMLAgilityPack解析HTML时,我得到NullExceptionError。在c中解析Outlook消息#
以下是我使用“查看源代码”从Outlook获取的消息。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head>
<body style="font-family: arial; font-size: 10pt;">
<br><br>
<table border="1" width="100%">
<tr>
<td width="100%" style="font-weight: bold;" bgcolor="#0066CC" colspan="2">
<font color="white">
<p align="center"><b>
MAIL2FAX DETAILED DELIVERY REPORT
</b></td>
</font>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Attention</td>
<td width="70%"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Job Number</td>
<td width="70%">48762955</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Sent By User </td>
<td width="70%"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Entered Fax2Mail System </td>
<td width="70%">03/28 13:02</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Report Generated</td>
<td width="70%">03/28 13:33</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Subject</td>
<td width="70%"> Requests: 1</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Page Count</td>
<td width="70%">
2 (including cover sheet)
</td>
</tr>
</table>
<br><br>
<table border="1" width="50%">
<tr bgcolor="#0066CC">
<td width="100%" colspan="3">
<p align="center" style="font-weight: bold;"><font color="white">SUMMARY</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="33%" style="font-weight: bold;"><p align="center">Sent: </b> 0</td>
<td width="33%" style="font-weight: bold;"><p align="center">Errors: </b> 1</td>
<td width="34%" style="font-weight: bold;"><p align="center">Cancelled: </b> 0</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="33%" style="font-weight: bold;"><p align="center">Total: </b> 1</td>
<td width="67%" colspan="2"> </td>
</tr>
</table>
<br><br>
<table border="1" width="100%">
<tr style="font-weight: bold;" bgcolor="#0066CC">
<td width="40%"><font color="white">Destination</td>
<td width="15%"><font color="white">Status</td>
<td width="15%"><font color="white">Date</td>
<td width="15%"><font color="white">Time</td>
<td width="15%"><font color="white">Num. Retries</td>
</tr>
</font>
<tr bgcolor="#99CCCC">
<td width="40%"><p align="left">1111111111 </td>
<td width="15%"><p align="left">ERR </td>
<td width="15%"><p align="left">03/28</td>
<td width="15%"><p align="left">13:33</td>
<td width="15%"><p align="right"> 4</td>
</tr>
</table>
</body>
</html>
这里是我的代码
Outlook.MailItem faxReport = Item as Outlook.MailItem;
(faxReport.Subject.IndexOf("Uploaded") == -1)
{
HtmlAgilityPack.HtmlDocument faxDoc = new HtmlAgilityPack.HtmlDocument();
faxDoc.LoadHtml(faxReport.Body);
HtmlNode tableNode = faxDoc.DocumentNode.SelectSingleNode("//table");
foreach (HtmlNode row in tableNode.SelectNodes("tr"))
{
HtmlNodeCollection cells = row.SelectNodes("th|tr");
MessageBox.Show("row and cells found");
foreach(HtmlNode cell in cells)
{
MessageBox.Show(cell.InnerText);
}
}
}
我不知道是否它给我这个错误信息的原因faxReport.Body是字符串类型,并且不显示HTML标记,或者是因为我做错了什么?我怎样才能解决这个问题?
感谢您的帮助。
的【什么是一个NullReferenceException,如何解决呢?(可能重复http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception - 我怎么办 - 它 - )它 – tnw
它不是。我得到的NullReferenceException是因为tableNode为NULL,因为HTML解析器找不到“table”。 – user1828605
调试你的代码,把断点,看看'faxReport.Body'是否包含预期的字符串值 – har07