C#:xml CData 中的字节数组
我有一个 WCF 服务返回 xml 块。 其中一个元素是 CData[] 部分。 我的应用程序从数据库中读取 HTML 文件,并使用 ABCPDF 将其转换为 PDF byte[] 数组。 然后在我的 XmlWriter 中,我将字节添加到 CData 部分。
问题是生成的 xml 如下所示:
<![CDATA[System.Byte[]]]>
如何将字节字符串放入 CData 部分? 我尝试过类似的事情:
string str;
ASCIIEncoding enc = new ASCIIEncoding();
str = enc.GetString(theData);
但
Convert.ToBase64String(theData);
我被困住了! 任何帮助都会很棒,谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以控制两端,那么使用
Convert.ToBase64String(data)
绝对是此处的方法。 您不想发送“原始”字节并假装它们是有效的文本数据。 在另一侧使用Convert.FromBase64String(text)
。不过,令我有点惊讶的是 WCF 没有自动为您处理此问题。 我不能说我自己使用过它(Marc Gravell 可能会突然出现 - 我相信他对此有很多经验),但我希望它只公开字节数组。 您为什么参与 XML 级别?
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. UseConvert.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?
很难准确说出问题发生的位置 - 更完整的代码示例可能会有所帮助。 但是从您显示的序列化情况来看,它看起来非常像在您的
byte[]
上调用ToString()
。如果您尝试通过线路发送二进制数据,您应该考虑使用Convert.ToBase64String()。 如果您无法控制接收格式,则需要了解它需要什么编码。
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 yourbyte[]
.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.