Hi, I'm getting an error when trying to deserialize my XML file.
The error message is "There is an error in XML document (2, 2)."

Below is the code in question and the XML file.

Could someone please explain why this is?

Thanks in advance!

<?xml version="1.0" encoding="utf-8"?>
<Objects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Object>
    <ObjectID>045410</ObjectID>
    <ObjectName>Circle</ObjectName>
    <ObjectColor>Blue</ObjectColor>
  </Object>
  <Object>
    <ObjectID>04010</ObjectID>
    <ObjectName>Square</ObjectName>
    <ObjectColor>Green</ObjectColor>
  </Object>
</Objects>

The file manager class

public List<Objects> loadObs(string theFileName)
        {
            List<Objects> obs;

            XmlSerializer serializer = new XmlSerializer(typeof(List<Objects>));     

            StreamReader reader = new StreamReader(theFileName);                       

            obs = (List<Objects>)serializer.Deserialize(reader);                    

            reader.Dispose();                                                       

            return obs;
        }

The Objects class

public class Objects
    {

        [XmlElement("ObjectID")]
        public string ObjectID { get; set; }
        [XmlElement("ObjectColor")]
        public string ObjectColor { get; set; }
        [XmlElement("ObjectName")]
        public string ObjectName { get; set; }

        public override string ToString()
        {
            return ObjectName + " " + ObjectColor;
        }
    }

Edited 4 Years Ago by venet: Mistake!

never mind.

turns out you need to say <ArrayOfObjects> in the xml file..

This article has been dead for over six months. Start a new discussion instead.