C
C#10mo ago
Astartechie

Issue with XmlReader

I am having an issue with XmlReader skipping the contents of a file. If I uncomment the Thread.Sleep, it works perfectly fine. Am I doing something wrong or should this work?
public void Import()
{
_logger.LogInformation("Beginning Import.");
var fs = new FileStream(@"test.xml", FileMode.OpenOrCreate,
FileAccess.Read, FileShare.Read);

using var reader = XmlReader.Create(fs);
while (!reader.EOF)
{
if (reader.Read())
{
_logger.LogInformation($"Type: {reader.NodeType}, Name: {reader.Name}, Value: {reader.Value}");
}
else
{
_logger.LogError("COULDNT READ");
}
}
//Thread.Sleep(1000); //Only Works when sleeping
}
public void Import()
{
_logger.LogInformation("Beginning Import.");
var fs = new FileStream(@"test.xml", FileMode.OpenOrCreate,
FileAccess.Read, FileShare.Read);

using var reader = XmlReader.Create(fs);
while (!reader.EOF)
{
if (reader.Read())
{
_logger.LogInformation($"Type: {reader.NodeType}, Name: {reader.Name}, Value: {reader.Value}");
}
else
{
_logger.LogError("COULDNT READ");
}
}
//Thread.Sleep(1000); //Only Works when sleeping
}
8 Replies
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
leowest
leowest10mo ago
is he not missing closing the FileStream or just using var fs
Bailey
Bailey10mo ago
thats correct. The sleep is just a time wich will cause different computers give different results. another option would be: using (var fs = new FileStream(@"test.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); { using var reader = XmlReader.Create(fs); while (!reader.EOF) { if (reader.Read()) { _logger.LogInformation($"Type: {reader.NodeType}, Name: {reader.Name}, Value: {reader.Value}"); } else { _logger.LogError("COULDNT READ"); } } } if you dont close, the io is different by speed on different computers Which will result with fast IO that the break in the data is o a different location. And on some computers the sleep 1000 could not be enough the IO caches the data and then sends it to the program. So if you have a fast computer. the IO data will be transferred faster. On a slow computer the IO will go slower. Which result in different ways Keeping this in mind. The result will be different on different computers and also depends on other processes which run. In this case the sleep is enough to get all. but in other cases it wont sdo keeping it open is a bad idea indeed The point is simple. You have to wait to get the last part of the bytes Dont know not my app
Bailey
Bailey10mo ago
but if you want to know more about the IO https://learn.microsoft.com/en-us/dotnet/standard/io/
File and Stream I/O - .NET
Learn the basics of file and stream I/O, which is the transfer of data either to or from a storage medium, in .NET.
Bailey
Bailey10mo ago
Asynchronous File I/O - .NET
Read about asynchronous file I/O in .NET. Learn async methods to simplify asynchronous operations, such as ReadAsync, WriteAsync, and more.
Bailey
Bailey10mo ago
If you dont want to block you can make it completely async or event based (a bit of the same) I cant explain everything on the site. So I try to give a direction in this case. But the buffer and how it works (complete story with IO) is very extended Sorry who do you think you are answering. The question originally was made by: Astartechie I tried to help with some extended answer see above I also agreed with you, but I also tried to explain why
leowest
leowest10mo ago
the OP issue seems just that he forgot to close the FileStream causing the issue
Bailey
Bailey10mo ago
ah. Ok then leavew this one. Best regards
Want results from more Discord servers?
Add your server