C
C#4d ago
Buck3tt

Fetching XML files from Azure Storage Explorer sometime fails

So for work we have a Azure Storage Explorer Fileshare where the customer sends XML files. Our Job fetches files from this fileshare every ~20 minutes maybe. Some files works fine and some just straight up fails with this code
var xmlFileByteArray = _azureHelper.GetFileBytes(fileName, sourceDirectory);

if (xmlFileByteArray is null)
{
return;
}

var reader = new StreamReader(new MemoryStream(xmlFileByteArray));
var xmlDocument = new XmlDocument();
xmlDocument.Load(reader);
var xmlFileByteArray = _azureHelper.GetFileBytes(fileName, sourceDirectory);

if (xmlFileByteArray is null)
{
return;
}

var reader = new StreamReader(new MemoryStream(xmlFileByteArray));
var xmlDocument = new XmlDocument();
xmlDocument.Load(reader);
And xmlDocument just says
System.Xml.XmlException: 'Data at the root level is invalid. Line 1, position 1.'
System.Xml.XmlException: 'Data at the root level is invalid. Line 1, position 1.'
The weird part is, when I locally debug it, fetch the correct file and run the same code, it doesn't fail. How could this be? They are named "123-1345.xml" or similar in the share and looks fine in notepad ++ or whatever program. Could there be some sort of "buffer" time that the file hasn't fully transformed in to a xml file yet or similar? Any help is appriciated
3 Replies
canton7
canton74d ago
_azureHelper is a class which you wrote? What does GetFileBytes do? Can you post it, see if we can spot anything obviously wrong? It'd probably be worth logging at least the first few bytes of the file if the parsing fails, so you have something to go on
Buck3tt
Buck3ttOP4d ago
Ye sorry. This is the method:
public byte[] GetFileBytes(string fileName, ShareDirectoryClient sourceDirectory)
{
try
{
var fileClient = sourceDirectory.GetFileClient(fileName);
var fileByteArray = Download(fileClient.Download());

if (fileByteArray is null)
{
Log.Error($"{nameof(GetFileBytes)}: Downloading of file failed, returning.");
return null;
}

return fileByteArray;
}
catch (Exception ex)
{
Log.Error($"Something went wrong in {nameof(GetFileBytes)}: {ex.Message}.");
return null;
}
}
public byte[] GetFileBytes(string fileName, ShareDirectoryClient sourceDirectory)
{
try
{
var fileClient = sourceDirectory.GetFileClient(fileName);
var fileByteArray = Download(fileClient.Download());

if (fileByteArray is null)
{
Log.Error($"{nameof(GetFileBytes)}: Downloading of file failed, returning.");
return null;
}

return fileByteArray;
}
catch (Exception ex)
{
Log.Error($"Something went wrong in {nameof(GetFileBytes)}: {ex.Message}.");
return null;
}
}
What we do as well if something fails, we move it to an error folder. But if I move a file back (that should work), it works
canton7
canton73d ago
Can you post the other two methods that calls? Trying to find the actual downloady logic

Did you find this page helpful?