Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Deserialization problem
    text
    copied!<p>I have the following class:</p> <pre><code>/// &lt;remarks/&gt; [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:eu.emsa.ssn")] public partial class SSN_ReceiptType { private Header1Type headerField; /// &lt;remarks/&gt; [System.Xml.Serialization.XmlElementAttribute(Order=0)] public Header1Type Header { get { return this.headerField; } set { this.headerField = value; } } } </code></pre> <p>And the following XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;SSN_Receipt xmlns="urn:eu.emsa.ssn"&gt; &lt;Header StatusMessage="SomethingSomething" StatusCode="Blabla" SSNRefId="N/A" MSRefId="2674762" Version="2.0" To="NCANOHAU1" SentAt="2010-11-12T14:48:44Z" From="SSN"/&gt; &lt;/SSN_Receipt&gt; </code></pre> <p>And I use this typed method to deserialize (by calling Deserialize&lt;SSN_ReceiptType&gt;(xmlGoesHere)):</p> <pre><code> /// &lt;summary&gt; /// Deserialize xml string to object of type T /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="xml"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static T Deserialize&lt;T&gt;(string xml) { try { // Create serializer var xs = new XmlSerializer(typeof(T)); // Deserialize T t = (T)xs.Deserialize(new StringReader(xml)); return t; } catch (Exception e) { log.Error(string.Format("Unable to deserialize XML: {0}", xml), e); return default(T); } } </code></pre> <p>Now here's the kicker. This used to work. But recently we received a new version of the XSD used to generate the classes, and after generating the new code (which is unchanged for this class, and seen above) I receive the following exception when trying to deserialize:</p> <blockquote> <p>System.SystemException: "There is an error in XML document (1, 57)"</p> </blockquote> <p>And furthermore:</p> <blockquote> <p>System.InnerException: &lt;SSN_Receipt xmlns='urn:eu.emsa.ssn'&gt; was not expected.</p> </blockquote> <p>What the hell? :S I tried adding an XmlRootAttribute specifying "SSN_Receipt", that did not help one bit. Anyone know what's going on?</p> <p>Update: relevant sections from the XSD:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ssn="urn:eu.emsa.ssn" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:eu.emsa.ssn"&gt; [...] &lt;xsd:element name="SSN_Receipt" type="ssn:SSN_ReceiptType"/&gt; [...] &lt;xsd:complexType name="SSN_ReceiptType"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation xml:lang="en"/&gt; &lt;/xsd:annotation&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="Header" type="ssn:Header1Type"/&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload