exception

来源:互联网 发布:易语言ftp源码 编辑:程序博客网 时间:2024/04/26 04:49

 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.