Search code examples
c#datatablexmlreader

Test for Empty file when using the DataTable.ReadXml() method


So I am using the DataTable.ReadXml() method described here like so:

DataTable actions = new DataTable();
string myfile = @".\MyFile.xml"
actions.ReadXml(file);

but if .\MyFile.xml is an empty file the method crashes.. how can I test whether the file is empty before passing it to the ReadFile() method


Solution

  • Check the file length before reading, as

    // Create new FileInfo object and get the Length.
    FileInfo f = new FileInfo(fileName);
    long s1 = f.Length;