Write Binary Data into XML with Linq

Posted on 2009-04-30
Last Modified: 2013-12-17
I read some values of the registry and write them to a XML with Linq. Some Values have binary characters and look like this:


The program crashes when I try to write those values into the XML. How can I write these values into the xml?
Question by:schubduese
    LVL 18

    Expert Comment

    You will want to MIME encode any binary data you store in an XML document.  See

    I use one of the components in IP!Works ( to do MIME encodings.  If you want to uuencode it instead, there is code here

    LVL 9

    Accepted Solution

    Simplest way is to base64 encode the data and then set it as element text. Convert.ToBase64String and Convert.FromBase64String should get you started.

    Author Comment

    Could you give me a C# example?

    Do I have to check every reg entry I read for bin values?

    Author Comment

    I can write the Data Base64 Encoded with this:

            public static string Encode(string str)
                byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
                return Convert.ToBase64String(encbuff);

    That means the routine wich Shows the XML in the Application needs this routine:

            public string Decode(string str)
                byte[] decbuff = Convert.FromBase64String(str);
                return System.Text.Encoding.UTF8.GetString(decbuff);


    That means writing the binary into the XML as it is, doesn't work?

    Author Comment

    There we go with the next Error Message

    The Key which failed, works now. But if I scan HKLM\Software\Windows\CurrentVersion it fails and says that the binary value (hex code 0xFFFF) is not a valid sign!

    Any help on this?

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now