views:

1398

answers:

2

Hi all,

I have a WCF service that is returning a block of xml. One element is a CData[] section. My application reads an HTML file out of the database and converts it to a PDF byte[] array using ABCPDF. Then in my XmlWriter Im adding the bytes to the CData section.

The problem is the resulting xml looks like this:

<![CDATA[System.Byte[]]]>

How can I get the string of bytes into the CData section? I've tried things like:

string str;
ASCIIEncoding enc = new ASCIIEncoding();
str = enc.GetString(theData);

and

Convert.ToBase64String(theData);

Im stuck!! Any help would be great, thank you!

+2  A: 

Using Convert.ToBase64String(data) is definitely the way to go here if you've got control of both ends. You don't want to be sending down "raw" bytes and pretending they're valid text data. Use Convert.FromBase64String(text) at the other side.

I'm slightly surprised that WCF isn't handling this for you automatically though. I can't say I've used it myself (Marc Gravell might pop in - he's got a lot of experience with it, I believe) but I'd expect it to just expose byte arrays. Why are you involved at the level of the XML?

Jon Skeet
Specifically many control characters are not allowed in XML documents, including nul (0).
Richard
Convert.ToBase64String() works now, I was still returning the byte array in my method. Grrrr!!! Thank you...
Blaze
A: 

It's hard to say exactly where your problem's happening - a more complete code example might help. But from what you show as getting serialized - it looks an awful lot like ToString() is being called on your byte[].

You should look into using Convert.ToBase64String() if you are trying to send binary data over the wire. If you are not in control of the receiving format, you need to look into what encoding it requires.

LBushkin