System.Xml

XmlNodeReader.ReadElementContentAsBinHex Method

Method

public byte[] ReadElementContentAsBinHex ()

Remarks

Reads the text content of the current node as binary data (BinHex encoded). This method can only be called when the current node is an element or attribute. It reads all the text content, including mixed content, and returns it as a byte array. The reader is positioned at the node following the element.

The BinHex encoding represents binary data using a subset of ASCII characters. This method decodes that representation into its original binary form.

If the element contains child elements or other nodes that are not text, this method will throw an InvalidOperationException. Mixed content (text interspersed with elements) is generally not supported by this method for direct BinHex decoding.

This method is useful for reading binary data that has been embedded within XML as BinHex encoded text content.

Parameters

This method does not take any parameters.

Return Value

A byte[] containing the decoded BinHex data from the element's content.

If the element is empty, an empty byte array is returned.

Exceptions

InvalidOperationException
Thrown if the reader is not positioned on an element or attribute node, or if the current node contains child nodes other than text nodes.
XmlException
Thrown if the content is not valid BinHex encoded data.

Example

The following example demonstrates how to use the ReadElementContentAsBinHex method to read BinHex encoded binary data from an XML element.


using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        string xmlString = @"
            <root>
                <binaryData>48656c6c6f20576f726c64</binaryData>
            </root>";

        using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString)))
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "binaryData")
                {
                    // Move to the content of the element
                    reader.Read(); 
                    
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        try
                        {
                            // Read the BinHex encoded content as bytes
                            byte[] binaryContent = reader.ReadElementContentAsBinHex();
                            
                            Console.WriteLine("Decoded Binary Data:");
                            foreach (byte b in binaryContent)
                            {
                                Console.Write(b.ToString("X2") + " ");
                            }
                            Console.WriteLine();
                        }
                        catch (XmlException ex)
                        {
                            Console.WriteLine($"Error decoding BinHex: {ex.Message}");
                        }
                        catch (InvalidOperationException ex)
                        {
                            Console.WriteLine($"Invalid operation: {ex.Message}");
                        }
                    }
                }
            }
        }
    }
}