3

Consider a .NET client calling into a remote XML SOAP web service that's defined as a Web Reference. The client calls the service, and is attempting to deserialize the return XML into an object.

The web method is executing properly, and the XML returned from the service is well formed, confirmed in Fiddler. The web service is a known good working service, as we have another client (.NET 2.0) consuming it without issue.

When the .NET web service proxy classes attempt to deserialize the returned XML, this error is thrown:

System.InvalidOperationException: There is an error in XML document (1, 1999). ---> System.Xml.XmlException: 'EndElement' is an invalid XmlNodeType. Line 1, position 1999. at System.Xml.XmlReader.ReadStartElement()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader_PRPA_AR101202_Service.Read135_MCCI_MT000200LocatedEntity(Boolean isNullable, Boolean checkType)

The line/column (1,1999) of the XML response that it's complaining about is the / in </device>:

<sender>
   <device>
      ..snip..
      <location classCode="IDENT" realmCode="xyz" />
   </device>

How can I resolve this issue with .NET deserialization?

Attempted solutions with no resolution / more information:

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Is there a corresponding (and preceding) ? – ChrisBint Mar 04 '13 at 22:46
  • @ChrisBint his code includes a corresponding and preceding . Hopefully the ..snip.. doesn't contain an extra – csj Mar 04 '13 at 23:17
  • @p.campbell Is that the whole error that is being thrown or is it just a part of it? – mostruash Mar 04 '13 at 23:23
  • I opened the full document link, viewed it raw, copied to Notepad++ and linearised the indented text. This converted it to one line, which is only 1865 characters. But the error is at line 1, character 1999! I suspect there is less text than the deserialiser expects, ie. a buffer usage miscalculation. Can you post the deserialisation code? – groverboy Mar 04 '13 at 23:35
  • OK so 'full document' is really 'de-identified full document' :-) – groverboy Mar 04 '13 at 23:51
  • The XML contains two `device` elements, where only the second of these contains an element `location` just before the closing `` tag. Is something amiss with the schema? Is .NET 4/4.5 more strict than 2.0 by default about matching XML to schema? – groverboy Mar 04 '13 at 23:59
  • Oh but the `device` elements are sub-elements of `receiver` and `sender` respectively. So I suppose the problem is elsewhere. – groverboy Mar 05 '13 at 00:04

2 Answers2

1

The second line of the stacktrace suggests that the xml processor expects an element locatedEntity inside the second device element. the earliest position at which it can be deduced that there will be no such element is the closing tag of said device element.

have a look at the schema document for namespace "urn:hl7-org:v3"; maybe location has been used erroneously instead of locatedEntity ?

collapsar
  • 17,010
  • 4
  • 35
  • 61
0

I used a .config tweak to force the XML serializer to use legacy behaviour:

<configuration>
     <system.xml.serialization> 
        <xmlSerializer  useLegacySerializerGeneration="true" />    
     </system.xml.serialization>
</configuration>

Serialization or deserialization failures when you run existing XML serialization code in WCF 4.5

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • So they optimized the `XmlSerializer` in 4.5 but provided a way not to use it. Does this mean there's a bug in the 4.5 `XmlSerializer` or that there's something wrong with the XML? – groverboy Mar 12 '13 at 00:14
  • @groverboy for sure the XML i was deserializing was valid. It was an existing working WS that was being consumed by a new client. So you've hit a good question: why did they keep a legacy switch? The other piece is the Web Reference that's assumed a particular object model. Perhaps the 'old style' WR expects an input different than the 4.5 'new' serializer can output. I'm happy it was a config setting that solved my problem. – p.campbell Mar 12 '13 at 04:05