0

I got an error trying to deserialize an xml to a class in .NET. I took an xml file and create an xsd from it using .net xsd tools, Then i created the class from the xsd i generated with the same tool.

I am getting this excpetion : 'Object cannot be stored in an array of this type' and 'There is an error in XML document (8, 144)'. In that line in the xml i got this :

 <events>
        <event assist="" assistid="" extra_min="" id="21775794" minute="87"    player="O. Atia" playerid="" result="[0 - 1]" team="away" type="goal"/>
</events>

this is the generated c# property:

[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("event", typeof(livescoreLeagueMatchEventsEvent), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    public livescoreLeagueMatchEventsEvent[][] events {
        get {
            return this.eventsField;
        }
        set {
            this.eventsField = value;
        }
    }

this is the parsing code:

XmlSerializer deserializer = new XmlSerializer(typeof(T));
using (XmlReader reader = XmlReader.Create(path))
{
    return (T)deserializer.Deserialize(reader);
}

This is the generated xsd :

            <xs:element name="events" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="event" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:attribute name="assist" type="xs:string" />
                      <xs:attribute name="assistid" type="xs:string" />
                      <xs:attribute name="extra_min" type="xs:string" />
                      <xs:attribute name="id" type="xs:string" />
                      <xs:attribute name="minute" type="xs:string" />
                      <xs:attribute name="player" type="xs:string" />
                      <xs:attribute name="playerid" type="xs:string" />
                      <xs:attribute name="result" type="xs:string" />
                      <xs:attribute name="team" type="xs:string" />
                      <xs:attribute name="type" type="xs:string" />
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>

I thought the xsd tools are creating correct class structure, and thought it will work without any change in code at all. Tried to step into the serializer deserialize method without any success...

3
  • What auto generated that? And btw its a property not a type... Looking at your xml it shouldn't be a two dimensional array but a single dimensional array. livescoreLeagueMatchEventsEvent[] Commented Apr 11, 2015 at 15:15
  • I used the command "xsd xmlfile.xml" to generate the xsd from my xml and then the command "xsd xsdfile.xsd /classes" to generate the c# class using the XML Schema Definition Tool from the command line. Commented Apr 11, 2015 at 15:21
  • Could you show the XSD? Is the multi-dimensional array in the generated code correct? (I'm asking because I recall a bug in xsd.exe that produced one array dimension too many in some cases. Don't remember the specifics right now.) Commented Apr 11, 2015 at 15:30

3 Answers 3

1

I just fixed my xsd which i figured was not correct for my xml specification. i changed the 'events' container element to be like this

 <xs:element name="events" minOccurs="0" maxOccurs="1"> 
 ..
 </xs:element>

and the new property in the generated class was what i needed :

public livescoreLeagueMatchEvent[] events {
    get {
        return this.eventsField;
    }
    set {
        this.eventsField = value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Should be as simple as this looking at the sample code you have shown us...

[Serializable]
public class MyClass 
{
    [XmlArrayItem("event", IsNullable = false)]
    public LivescoreLeagueMatchEventsEvent[] events { get; set; }
}

Comments

0

You are running into a known bug in xsd.exe:
http://webservices20.blogspot.de/2010/02/net-wcf-bug-cannot-convert-type-to.html

Use one of the following workarounds:

  • Edit the generated proxy class - remove one occurrence of [].

  • Edit the XSD by adding a dummy element <xs:element minOccurs="0" name="dummy" type="xs:string"/> below the definition of the event element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.