只创建了一个文件 为什么?
在这里,我从数据库读取数据,循环结果,并将包含 HTML 字符串行的列值传递给函数。此函数从 HTML 输入中提取 标记和内容。
当添加断点进行调试并且我单步执行代码时,一切正常。当断点被移除时,只创建一个文件,但不会抛出异常。代码:
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
try
{
getBody(ds.Tables[0].Rows[i]["MailText"].ToString());
}
catch(Exception exp)
{
Response.Write(exp.ToString());
}
}
public void getBody(string html)
{
try
{
HtmlDocument HD = new HtmlDocument();
HD.LoadHtml(html);
string output = HD.DocumentNode.SelectSingleNode("//body") == null ? HD.DocumentNode.InnerHtml : HD.DocumentNode.SelectSingleNode("//body").InnerHtml;
using (StreamWriter sw = new StreamWriter(Server.MapPath("OnlyBody/") + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".txt"))
{
sw.Flush();
sw.Write(output);
if (sw != null)
{
sw.Close();
sw.Dispose();
}
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
}
}
起初我以为问题出在文件流上,但即使在处理 StreamWriter
之后,错误仍然存在。
Here I read data from database, loop over results, and pass a column value containing an HTML string row to a function. This function extracts <body>
tag and contents from HTML input.
When a breakpoint is added to debug and I step through code, everything works properly. When breakpoint is removed, only one file is created but no exceptions are thrown. Code:
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
try
{
getBody(ds.Tables[0].Rows[i]["MailText"].ToString());
}
catch(Exception exp)
{
Response.Write(exp.ToString());
}
}
public void getBody(string html)
{
try
{
HtmlDocument HD = new HtmlDocument();
HD.LoadHtml(html);
string output = HD.DocumentNode.SelectSingleNode("//body") == null ? HD.DocumentNode.InnerHtml : HD.DocumentNode.SelectSingleNode("//body").InnerHtml;
using (StreamWriter sw = new StreamWriter(Server.MapPath("OnlyBody/") + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".txt"))
{
sw.Flush();
sw.Write(output);
if (sw != null)
{
sw.Close();
sw.Dispose();
}
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
}
}
At first I thought the problem was with file stream, but even after disposing StreamWriter
the error persists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用断点进行调试时,此代码块将返回不同的结果:
如果没有断点,秒 (
ss
) 不会增加,因为代码执行得太快。When you debug with breakpoints, this block of code will return different results:
Without breakpoints, the seconds (
ss
) don't increment because the code is executing too quickly.