2

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!

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
Blaze
  • 1,863
  • 7
  • 23
  • 40
  • What are you getting in your output when you use Convert.ToBase64String? That's probably the best way to do it (using ASCIIEncoding.GetString only works if the bytes represent a valid ASCII string). – Jonathan Rupp Jun 08 '09 at 12:30

2 Answers2

7

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Specifically many control characters are not allowed in XML documents, including nul (0). – Richard Jun 08 '09 at 12:30
  • Convert.ToBase64String() works now, I was still returning the byte array in my method. Grrrr!!! Thank you... – Blaze Jun 08 '09 at 12:53
0

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
  • 129,300
  • 32
  • 216
  • 265