exception

 Let me clarify this a bit....
This post is a bit old and I assumed it fell into some kind of a forum black hole.  I've found out what the problem actually is.  I ended up having to do a work-around, though.

The original error was Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''
this means that when the message was being deserialized, it was expecting an element instead of a string.  For instance, instead of:
<hello><world>x</world></hello> it got something like <hello>world</hello> (the exception would occur when processing the hello element).

This was being caused by the fact that one of the members of serialized class was a byte array. Something along the lines of:

[DataContract()]
class Person{
   [DataMember()]
   public string Name{get;set;}

   [DataMember()]
   public byte[] Photo{get;set;}
   
}

Now, here's the kicker....by default, when an array of some primitive type is serialized, you end up with child elements whose names are that type...it loooks like:

<Person>
   <Name>Amanda Reconwith</Name>
   <Photo>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
      <byte>34</byte>
   </Photo>
</Person>

however, the serializer was smart and turned it into a Base64 encoded string

<Person>
   <Name>Chanda Leir</Name>
   <Photo>YTM0NZomIzI2OTsmIzM0NTueYQ==</Photo>
</Person>


So, the exception makes total sense.  The de-serialization process is looking for a child element named 'byte' but encounters a string.
I appreciate that the serializer had the intelligence to do that, but its rather useless (dangerous) if the serializer is unable to de-serialize the message that it created (its looking for /Person/Photo/byte).

I ended up changing the data type to string and base64 encoded it myself.

Now, if anyone knows how to make a byte array, as a DataMember work, without the auto-magic encoding messing up, I would be very interested in hearing about that.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值