Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing classes from XML generated using XSD.exe
    primarykey
    data
    text
    <p>I have classes generated (using xsd.exe) from an .xsd that I can serialize just fine, but when I try and deserialize it, I get the error:</p> <pre><code>{"&lt;XMLLanguages xmlns='http://tempuri.org/XMLLanguages.xsd'&gt; was not expected."} </code></pre> <p>I've searched for a couple of hours and found most peoples problems lie in not declaring namespaces in their xsd/xml, not defining namespaces in their classes, etc, but I can't find a solution for my problem. </p> <p>Here are code snippets for the relevant classes.</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xs:schema id="XMLLanguages" targetNamespace="http://tempuri.org/XMLLanguages.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLLanguages.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" &gt; &lt;xs:element name="XMLLanguages"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="Tier" minOccurs="1" maxOccurs="unbounded"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="L" minOccurs="1" maxOccurs="unbounded" type="Language"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="TierID" type="xs:int"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:complexType name="Language"&gt; &lt;xs:sequence&gt; &lt;xs:element name="LangID" type="xs:int"/&gt; &lt;xs:element name="Tier" type="xs:int"/&gt; &lt;xs:element name ="Name" type="xs:string"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name ="PassRate" type="xs:int"/&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>And the class:</p> <pre><code> /// &lt;remarks/&gt; [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/XMLLanguages.xsd")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLLanguages.xsd", IsNullable = false)] public partial class XMLLanguages { private List&lt;XMLLanguagesTier&gt; tierField; /// &lt;remarks/&gt; [System.Xml.Serialization.XmlElementAttribute("Tier")] public List&lt;XMLLanguagesTier&gt; Tiers { get { return this.tierField; } set { this.tierField = value; } } } </code></pre> <p>And a the line in XML causing the error:</p> <pre><code>&lt;XMLLanguages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/XMLLanguages.xsd"&gt; </code></pre> <p>Deserializing method:</p> <pre><code>public static object Deserialize(XmlDocument xml, Type type) { XmlSerializer s = new XmlSerializer(type); string xmlString = xml.OuterXml.ToString(); byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString); MemoryStream ms = new MemoryStream(buffer); XmlReader reader = new XmlTextReader(ms); Exception caught = null; try { object o = s.Deserialize(reader); return o; } catch (Exception e) { caught = e; } finally { reader.Close(); if (caught != null) throw caught; } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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