Reputation: 770
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...
Upvotes: 0
Views: 699
Reputation: 770
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;
}
}
Upvotes: 1
Reputation: 1490
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.
Upvotes: 0
Reputation: 1977
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; }
}
Upvotes: 0