2

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.

Mihai Stan
  • 143
  • 1
  • 13

1 Answers1

1

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.

cyberj0g
  • 3,707
  • 1
  • 19
  • 34