tags:

views:

388

answers:

2

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:

{"<XMLLanguages xmlns='http://tempuri.org/XMLLanguages.xsd'&gt; was not expected."}

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.

Here are code snippets for the relevant classes.

    <?xml version="1.0" encoding="utf-8"?>
<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"
>
  <xs:element name="XMLLanguages">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Tier" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="L" minOccurs="1" maxOccurs="unbounded" type="Language"/>
            </xs:sequence>
            <xs:attribute name="TierID" type="xs:int"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Language">
    <xs:sequence>
      <xs:element name="LangID" type="xs:int"/>
      <xs:element name="Tier" type="xs:int"/>
      <xs:element name ="Name" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name ="PassRate" type="xs:int"/>
  </xs:complexType>
</xs:schema>

And the class:

    /// <remarks/>
[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<XMLLanguagesTier> tierField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Tier")]
    public List<XMLLanguagesTier> Tiers {
        get {
            return this.tierField;
        }
        set {
            this.tierField = value;
        }
    }
}

And a the line in XML causing the error:

<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;

Deserializing method:

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;
    }
A: 

Hi,

I use the following code and it works fine.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Report"
           targetNamespace="http://www.xyz.com/Report.xsd"
           elementFormDefault="qualified"
           xmlns="http://www.xyz.com/Report.xsd"
           xmlns:mstns="http://www.xyz.com/Report.xsd"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
 <xs:element name="Report">
  <xs:complexType>
  ...

I use xsd.exe with the /n:xyz namespace option.

This seems to work fine, so I assume your problem must be with the tempuri.org domain name.

Hope this is of some help.

Richard.

Richard Forss
The tempuri domain is the default domain for xsd files in VS2010, I tried another domain with fresh xml files and the error is identical, save for the new domain.
heap
I thought that the issue could have been that the classes were not defined as being in the same namespace as the xml, so changed the namespaces to all be the same. Still doesn't work.The NamespaceURI property of the XmlDocument, however, is "", I'm not sure if this is the issue.
heap
A: 

You need to remove the targetNamespace attribute from the root, and add the following node before the XMLLanguages node:

<xs:import namespace="http://www.w3.org/2001/XMLSchema"/&gt;

The above will let you deserialize, but I sense other problems on the horizon. The problem you were facing was that when you define multiple complex types, you can't use the targetNamespace attribute -- welcome to schema namespace hell...

code4life