English 中文(简体)
如果没有Xml文档
原标题:If xml file does not exist

我正在使用协会。 NET C#,并用XmlDataSource工具写了一页,读到Xml文档,并在GridView中显示数据。 在档案中,一切都很重要。 然而,每小时生成Xml文档,如果在档案更新时能与网页连接(约2分钟),就会发现错误(因为档案没有)。 由于我正在使用内在工具连接和阅读Xml,因此,如果档案存在,如果档案没有,我可以用来检查该网页。

I can see that there is code to do an if exists, however I can not seem to figure out the part "what to do?" to ignore the XMLDataSource tool. Perhaps I can make a label appear that says to come back in a few minutes, but how do I get it to ignore the data reader?

if (!File.Exists(filename))
{
     // what to do?
}  
问题回答

如果你试图以这种方式使用<代码>File.Exists,你将再次失望。

让我说,你有最简单的法典:

if (!File.Exists(filename))
{
    // Tell user that file isn t there.
}
else
{
    // The file exists, so now go party on it.
    DoSomething(filename);
}

因此,你的方案确定档案存在。 但是,在<代码>DoSomething之前,创建该档案的过程可以独家查阅。 您的<代码>DoSomething方法将失败。 因此,你对档案存在的检查是无关紧要的。

Yes, it s a very small window. I can tell you from experience that things do indeed happen within those very small windows. I ve been bitten by code similar to that above.

我强烈建议你写一下你的法典,以便处理在档案存在时被推翻的档案(或任何例外)。 例如:

try
{
    DoSomething(filename);
    // now format the page as normal
}
catch (FileNotFoundException)
{
    // notify user that the file wasn t found
}

否则,没有任何其他进程能够从你手中夺走。 你们知道,档案是在那里的,因为你已经打开。

这就是我的解决办法。 我将变数名称改为保护无辜者。 许多人感谢Jim Mischel......

protected void Page_Load(object sender, EventArgs e)
{
    string xmlFile = Server.MapPath("~/App_Data/file.xml");

    try
    {
        XmlDataSource1.DataFile = xmlFile;
        XmlDataSource1.GetXmlDocument();
    }
    catch (Exception ex)
    {
        lblErrorMessage.Visible = true;
        gridView1.Visible = false;
        formView1.Visible = false;
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签