0

i have the following xml structure im trying to deserialize into a c# object:

<leagueTable>
    <competition>Calor League Division One Central</competition>
    <description>League Table</description>
    <team>
        <position>1</position>
        <name>Barton Rovers</name>
        <played>13</played>
        <won>8</won>
        <drawn>3</drawn>
        <lost>2</lost>
        <for>25</for>
        <against>16</against>
        <goalDifference>+9</goalDifference>
        <points>27</points>
    </team>
    <team>
    ......
    </team>
    <team>
    .....
    </team>
</leagueTable>

and have created two classes for them to be deserialized into which are as follows

 public class leagueTable
    {
        public string competition { get; set; }
        public string description { get; set; }

        [XmlElement("team")]
        public List<team> TeamsList { get; set; }
    }

and

[Serializable()]
    public class team

    {

        public int position { get; set; }
        public String name { get; set; }
        public int played { get; set; }
        public int won { get; set; }
        public int drawn { get; set; }
        public int lost { get; set; }
         [XmlElement("for")]
        public int goalsfor { get; set; }
         [XmlElement("against")]
         public int goalsagainst { get; set; }
        [XmlElement("goalDifference")]
        public int goaldifference { get; set; }
        public int points { get; set; }

    }

What i cant seem to figure out is how to get the teams deserialized into a list object. When the deserialization happens the competition and description properties are populated, however the teamslist list object is null. i have tried a number of different ways to create the property but each either fail or just stay as null. I have followed a number of different posts on here and on the web but no matter what i try it still fails to be populated.

For completeness this is the deserialisation code, edited to show xml setting

        XmlSerializer serializer = new XmlSerializer(typeof(leagueTable));

        leagueTable league = null;

        Stream xmldoc = response.GetResponseStream();

            XmlReaderSettings xmlsettings = new XmlReaderSettings();
            xmlsettings.DtdProcessing = DtdProcessing.Parse;

            using (XmlReader reader = XmlReader.Create(xmldoc, xmlsettings))
            {
                league = (leagueTable)serializer.Deserialize(reader);
            }
Deza
  • 111
  • 2
  • 9
  • 1
    This question has already been answered: check out: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – Matrix8898 Oct 28 '13 at 23:28
  • I tested your code and it worked. Maybe there's something wrong with your serializer or your xmlsettings? How the serializer is created? What is the content of your xmlsettings? – AirL Oct 28 '13 at 23:31
  • Certainly not related but the provided xml file doesn't end with as it should be ;) – AirL Oct 28 '13 at 23:34
  • I dont think it has, i have followed the answers in that question and it hasnt helped, making me pull my hair out more is the fact it works for AirL. Will edit the closing tag, thats what you get for writing it by hand! – Deza Oct 29 '13 at 09:31
  • Ok, just tried this again and strangely it worked! not sure whats changed, but its working. Which also means the code above is correct. Is there a way i can close the question? – Deza Oct 29 '13 at 09:37

0 Answers0