检查流是否为空
我正在尝试反序列化 XML 文件。我需要在反序列化之前检查 XML 文件流是否为空。
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);
如何检查 isfs1
是否为空?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查
Length
属性溪流。如果为 0,则文件为空。
Check the
Length
property of the stream.If it is 0, the file is empty.
如果您的文件是 UTF-8 格式,由于 BOM(字节顺序标记),它的大小至少为 3。所以检查长度>即使文件为空,0 也会返回 true。
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.
IsolatedStorageFileStream.Length 可以吗?
Would IsolatedStorageFileStream.Length work?
如果构建空的 zip 存档内存流,它有 22 个字节处于空状态。
If you construct empty zip archive memory stream it have 22 bytes in empty state.
if isfs1.Length =0 表示流为空
if isfs1.Length =0 means the stream is empty