I have some code that is meant to retrieve a file from an S3 bucket and deserialize the file. When I use a file stream I get all the data no problem, but when I use a memory stream it seems like I am not getting all of the data:
It doesnt get the full XML:
Where it should look like:
Here is the code I am using:
internal object ReadDataContractFromFile(string filename, Type type)
{
GetObjectRequest getObjRequest = new GetObjectRequest();
MemoryStream memoryStream = new MemoryStream();
getObjRequest.BucketName = bucketName;
getObjRequest.Key = filename;
string path = @"C:\{PATH_TO_FILE}\requests\" + filename;
FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
using (GetObjectResponse getObjRespone = s3Client.GetObject(getObjRequest))
using (Stream responseStream = getObjRespone.ResponseStream)
{
responseStream.CopyTo(fileStream);
//memoryStream.Seek(0, 0);
XmlReaderSettings rs = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment,
};
XmlReader r = XmlReader.Create(fileStream, rs);
return new DataContractSerializer(type).ReadObject(r);
}
}
If I use memoryStream in place of the fileStream variable, I get the incomplete XML I showed above. I tried to seek to the beginning of the stream incase the position was wrong but that didnt fix it. Any idea what Im doing wrong?