• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1104
  • Last Modified:

Write Binary Data into XML with Linq

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?
  • 3
1 Solution
You will want to MIME encode any binary data you store in an XML document.  See http://www.xml.com/pub/a/98/07/binary/binary.html

I use one of the components in IP!Works (www.nsoftware.com) to do MIME encodings.  If you want to uuencode it instead, there is code here http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=351

Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
Simplest way is to base64 encode the data and then set it as element text. Convert.ToBase64String and Convert.FromBase64String should get you started.
schubdueseAuthor Commented:
Could you give me a C# example?

Do I have to check every reg entry I read for bin values?
schubdueseAuthor Commented:
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?
schubdueseAuthor Commented:
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now