Expand Minimize
This topic has not yet been rated - Rate this topic

XmlReader.ReadElementContentAsBase64 Method

Reads the element and decodes the Base64 content.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public virtual int ReadElementContentAsBase64(
	byte[] buffer,
	int index,
	int count
)

Parameters

buffer
Type: System.Byte[]

The buffer into which to copy the resulting text. This value cannot be null.

index
Type: System.Int32

The offset into the buffer where to start copying the result.

count
Type: System.Int32

The maximum number of bytes to copy into the buffer. The actual number of bytes copied is returned from this method.

Return Value

Type: System.Int32
The number of bytes written to the buffer.
ExceptionCondition
ArgumentNullException

The buffer value is null.

InvalidOperationException

The current node is not an element node.

ArgumentOutOfRangeException

The index into the buffer or index + count is larger than the allocated buffer size.

NotSupportedException

The XmlReader implementation does not support this method.

XmlException

The element contains mixed-content.

FormatException

The content cannot be converted to the requested type.

InvalidOperationException

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”

This method reads the element content, decodes it using Base64 encoding, and returns the decoded binary bytes (for example, an inline Base64-encoded GIF image) into the buffer. For more information, see RFC 1521, "MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies". You can obtain RFCs from the Request for Comments Web site.

ReadElementContentAsBase64 can only read simple-content elements. The element can contain text, white space, significant white space, CDATA sections, comments and processing instructions. It can also contain entity references, which are automatically expanded. The element cannot have child elements.

This method is very similar to the ReadContentAsBase64 method except that it can only be called on element node types.

If the count value is higher than the number of bytes in the document, or if it is equal to the number of bytes in the document, the XmlReader reads all the remaining bytes in the document and returns the number of bytes read. The next XmlReader method call returns a zero and moves the reader to the node following the EndElement.

If you call Read before all of the element content is consumed, the reader may behave as if the first content was consumed and then the Read method was called. This means that the reader will read all the text until the end element is encountered. It will then read the end tag node, read the next node, and then position itself on the next subsequent node.

For the asynchronous version of this method, see ReadElementContentAsBase64Async.

The following example reads an inline Base64 encoded image. The Base64 data is embedded within the <image> element. A BinaryWriter is used to create a new binary data file.


public static void Base64DecodeImageFile() {

  byte[] buffer = new byte[1000];
  int readBytes = 0;

  using (XmlReader reader = XmlReader.Create("output.xml")) {
					   
        FileStream outputFile = new FileStream(@"C:\artFiles\data\newImage.jpg",
					FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
        // Read to the image element.
        reader.ReadToFollowing("image");
        // Read the Base64 data.
        Console.WriteLine("\r\nReading Base64...");
        BinaryWriter bw = new BinaryWriter(outputFile);
        while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50))>0) {
            bw.Write(buffer, 0, readBytes);
        }
        outputFile.Close();
  }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.