Mihai Stan
Mihai Stan

Reputation: 153

Error Deserializing xml file with XmlSerializer

I'm trying to deserialize an xml with XmlSerializer and I get InvalidOperationException: was not expected. Here's the xml file:

<?xml version="1.0" encoding="us-ascii"?>
    <ArrayOfplatform xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <platform>
    <positionX></positionX>
    <positionY></positionY>
    <moveSpeed>10</moveSpeed>
    <ID>1</ID>
  </platform>
</ArrayOfplatform>

And the classes

public class platform : gameElement 
{
    //[Serializable] 
    private float moveSpeed;
    public int ID;
}
public class gameElement
{   
    //[Serializable]
    public float positionX, positionY;
}

If it matters, I'm trying to read stuff for a platformer game made in Unity.

Upvotes: 2

Views: 166

Answers (1)

cyberj0g
cyberj0g

Reputation: 3787

There're couple of problems with your XML:

  1. Array serialization format is wrong for XmlSerializer - element name should be ArrayOfPlatform instead of ArrayOfplatform. (uppercase P)
  2. platform.moveSpeed is private in your class and cannot be serialized or deserialized with XmlSerializer.
  3. positionX, positionY are of type float (value-type) and cannot have empty values in the XML. Should be 0.

Fix that, everything else is fine.

Upvotes: 1

Related Questions