Checking if a stream is empty

I am trying to deserialize a XML-file. I need to check if the XML-file stream is empty before tying to deserialize it.

IsolatedStorageFileStream isfs1 = new IsolatedStorageFileStream("test.xml", 
    FileMode.Open, FileAccess.Read, isf);

// Deserialize the XML to an object
Settings s = new Settings();
SoapFormatter SF= new SoapFormatter();
s = (Settings) SF.Deserialize(isfs1); 

How can I check if isfs1 empty or not?

Answers


Check the Length property of the stream.

Length represents the number of bytes currently in the file.

If it is 0, the file is empty.


If your file is in UTF-8 format, it's size is at least 3 because of BOM(Byte Order Mark). So checking the length > 0 will return true even if your file is empty.


if isfs1.Length =0 means the stream is empty


Would IsolatedStorageFileStream.Length work?

if (isfs1.Length > 0) {
  // isfs1 isn't empty.
}

Need Your Help

pcap.h header file problem

ubuntu-10.10

anyone know how to fix the problem regarding header file in ubuntu 10.10.

How to get the past Sunday and the coming Sunday in Java?

java date

I asked How to detect if a date is within this or next week in Java? but the answers were confusing, so now I think if I can find the past Sunday and the coming Sunday, any day in between is this w...