Search code examples
c#xmlarrayscdataabcpdf

C#: byte array in xml CData


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!


Solution

  • 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?