How to check if XML is fully written to the folder in C#

78 views
Skip to first unread message

Learner

unread,
Oct 30, 2013, 4:16:58 PM10/30/13
to dotnetde...@googlegroups.com
Hi,

The dataset in my application reads an xml from the network and uses that as a datasource to load the data. This XML file is generated every 15 mins (15 mins old data to the live). Users search against this xml file and I want to make sure that the xml file is ready and not still being written the to the network folder. If it still writing then I would like to put the thread to sleep for couple of seconds and try reading the file again. Here is what I currently have and this needs modification.

protected void BindData()

{

   try

      {

        DataView dvGridData;

          AXGridView.PageIndexChanging += new GridViewPageEventHandler(AXGridView_PageIndexChanging);

        AXGridView.PagerTemplate = null;

        DataSet dsGridData = new DataSet();

       dsGridData.ReadXml(Server.MapPath(FilePath));  //This is how I am reading XML file. This is where I need to test if it is written completely and if not need to wait for couple of seconds or until it is fully written.

      if (dsGridData.Tables.Count > 0)

          dvGridData = new DataView(dsGridData.Tables[0]);

      else

           dvGridData = new DataView();

      dvGridData.RowFilter = FilterExpression;

      AXGridView.DataSource = dvGridData;

      AXGridView.DataBind();

     }

    catch

    {

       throw;

    }

}


Thanks for the any help or code.


Thanks,


Dev

Stephen Russell

unread,
Oct 30, 2013, 4:40:02 PM10/30/13
to dotnetde...@googlegroups.com
I would attempt to change the file name as my test for write completed.  


--
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net
 
---
You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotnetdevelopm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Stephen Russell
Sr. Analyst
Ring Container Technology
Oakland TN

901.246-0159 cell

Learner

unread,
Oct 30, 2013, 4:46:56 PM10/30/13
to dotnetde...@googlegroups.com
Thanks for the quick reply. But not sure if I understand what you meant. Can you please elobarate or a code snippet helps..

Thank you,

Dev

Stephen Russell

unread,
Oct 30, 2013, 5:02:54 PM10/30/13
to dotnetde...@googlegroups.com


On Wed, Oct 30, 2013 at 3:46 PM, Learner <pra...@gmail.com> wrote:
Thanks for the quick reply. But not sure if I understand what you meant. Can you please elobarate or a code snippet helps..


Learner

unread,
Oct 30, 2013, 7:27:53 PM10/30/13
to dotnetde...@googlegroups.com
Hi,

  Try changing the file name might also work but I am putting the code like below. I debugged it and seems like working. But I couldn't simulate a scenario while the file is being written but I am assuming it should work. Please take a look at it.

protected void BindData()
        {
            try
            {
                DataView dvGridData;
                AXGridView.PageIndexChanging += new
                       GridViewPageEventHandler(AXGridView_PageIndexChanging);
                AXGridView.PagerTemplate = null;
                DataSet dsGridData = new DataSet();

                
                //start of new code
                
                while (IsFileLocked(dsGridData))
                {
                    System.Threading.Thread.Sleep(500);
                }
               //end of new code


                if (dsGridData.Tables.Count > 0)
                    dvGridData = new DataView(dsGridData.Tables[0]);
                else
                    dvGridData = new DataView();
                dvGridData.RowFilter = FilterExpression;
                AXGridView.DataSource = dvGridData;
                AXGridView.DataBind();
            }
            catch
            {
                throw;
            }
        }

protected Boolean IsFileLocked(DataSet dsGridData)
        {
            try
            {
               dsGridData.ReadXml(Server.MapPath(FilePath));
            }
            catch (IOException)
            {
                return true;
            }
            //file is not locked
            return false;
        }
Please provide if you have any comments on this.

Thanks,

Dev
Reply all
Reply to author
Forward
0 new messages