XmlMtomReader Reading Strategy

Consider the following code:

Stream stream = GetStreamFromSomewhere(); 
XmlDictionaryReader mtomReader =XmlDictionaryReader.CreateMtomReader
(
 stream,
 Encoding.UTF8,
 XmlDictionaryReaderQuoatas.Max
);

/// ...

/// is there best way to read binary data from mtomReader element??
string elementString = mtomReader.XmlReader.ReadElementString();
byte[] elementBytes = Covert.FromBase64String(elementString);
Stream elementFileStream = new FileStream(tempFileLocation);
elementFileStream.Write(elementBytes,0,elementBytes.Length);
elementFileStream.Close();

/// ...

mtomReader.Close();

      

The problem is that the size of the binary attachment is supposed to be more than 100 MB. Is there a way to read a block of a binary block of elements block by block and then write it to a temporary file stream so that I can avoid allocating memory for the hole stuff?

The second - even more specific issue - allows mtomReader to create any internal cache of the binary mime attachment before I read the content of the element i.e. allocates memory for binary data? Or does it read bytes directly from the input stream?

+2


source to share


3 answers


For those who might be interested in the solution:

using (Stream stream = GetStreamFromSomewhere())
{
  using (
    XmlDictionaryReader mtomReader = XmlDictionaryReader.CreateMtomReader(
        stream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max))
 {
    string elementString = mtomReader.ReadElementString();
    byte[] buffer = new byte[1024];
    using (
        Stream elementFileStream =
            new FileStream(tempFileLocation, FileMode.Create))
    {
        while(mtomReader.XmlReader.ReadElementContentAsBase64(buffer,0,buffer.Length)
        {
          elementFileStream.Write(buffer, 0, buffer.Length);
        }
    }

    /// ...

    mtomReader.Close();
 }
}

      



ReadElementContentAsBase64 (...) helps to read binary parts block by block. The second issue of my post was fully covered here: Does the XmlMtomReader internally cache binary data from the input stream?

+3


source


For an attachment of this size, it would be better to use live streaming.

Stream transfers can improve service scalability by removing the requirement for large memory buffers. Whether the transfer mode improves scalability depends on the size of the messages being transferred. Large message sizes are used by streaming.



See: http://msdn.microsoft.com/en-us/library/ms731913.aspx

0


source


First, your code should be something like this:

using (Stream stream = GetStreamFromSomewhere())
{
    using (
        XmlDictionaryReader mtomReader = XmlDictionaryReader.CreateMtomReader(
            stream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max))
    {
        string elementString = mtomReader.ReadElementString();
        byte[] elementBytes = Convert.FromBase64String(elementString);
        using (
            Stream elementFileStream =
                new FileStream(tempFileLocation, FileMode.Create))
        {
            elementFileStream.Write(
                elementBytes, 0, elementBytes.Length);
        }

        /// ...

        mtomReader.Close();
    }
}

      

Without blocks, using

you risk resource leaks.

0


source







All Articles