none
facing issue while XML deserialization. Getting null array

    Question

  • Hi, I have followed this article. I don't want to use any 3rd party lib to deserialize XML. My XML is like this

    <ArrayOfPartFlights xmlns="http://schemas.datacontract.org/2004/07/SaksoonAirport" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <PartFlights>
        <Flight>WS182</Flight>
        <Origin>Calgary</Origin>
        <Status>Delayed</Status>
        <Time>9:21 PM</Time>
      </PartFlights>
      <PartFlights>
        <Flight>9T301</Flight>
        <Origin>Stony Rapids</Origin>
        <Status>Delayed</Status>
        <Time>10:25 PM</Time>
      </PartFlights>
    </ArrayOfPartFlights>

    Here is my class structure

    public class PartFlights
    {
        [XmlElement("Flight", Namespace = "")]
        public string Flight { get; set; }
    
        [XmlElement("Origin", Namespace = "")]
        public string Origin { get; set; }
    
        [XmlElement("Status", Namespace = "")]
        public string Status { get; set; }
    
        [XmlElement("Time", Namespace = "")]
        public string Time { get; set; }
    }
    
    [XmlRoot("ArrayOfPartFlights", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
    public class Flights
    {
        [XmlElement("PartFlights", Namespace = "")]
        public PartFlights[] PartFlightsList { get; set; }
    }
    

    Event

    private async void btnDeserialize_Click(object sender, RoutedEventArgs e)
    {
        var sessionFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("test.xml");
        IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
    
        var serializer = new XmlSerializer(typeof(Flights), "http://schemas.datacontract.org/2004/07/SaksoonAirport");
        var _data = (Flights)serializer.Deserialize(sessionInputStream.AsStreamForRead());
        sessionInputStream.Dispose();
    }
    

    I am getting _data with PartFlightsList = null. So please can anyone tell me what's the problem in my code. Thanks.

    Thursday, April 04, 2013 6:52 AM

Answers

  • Change your classes as follows:

        public class PartFlights
        {
            [XmlElement("Flight", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Flight { get; set; }
    
            [XmlElement("Origin", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Origin { get; set; }
    
            [XmlElement("Status", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Status { get; set; }
    
            [XmlElement("Time", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Time { get; set; }
        }
    
        [XmlRoot("ArrayOfPartFlights", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
        public class Flights
        {
            [XmlElement("PartFlights", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public PartFlights[] PartFlightsList { get; set; }
        }

    then create the XmlSerializer as

    XmlSerializer ser = new XmlSerializer(typeof(Flights));

    Or don't use a namespace in the serialization attributes and use

        public class PartFlights
        {
            [XmlElement("Flight")]
            public string Flight { get; set; }
    
            [XmlElement("Origin")]
            public string Origin { get; set; }
    
            [XmlElement("Status")]
            public string Status { get; set; }
    
            [XmlElement("Time")]
            public string Time { get; set; }
        }
    
        [XmlRoot("ArrayOfPartFlights")]
        public class Flights
        {
            [XmlElement("PartFlights")]
            public PartFlights[] PartFlightsList { get; set; }
        }

    then set up the XmlSerializer as

    XmlSerializer ser = new XmlSerializer(typeof(Flights), "http://schemas.datacontract.org/2004/07/SaksoonAirport");


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Marked as answer by Xyroid Tuesday, April 09, 2013 5:01 AM
    Monday, April 08, 2013 5:01 PM

All replies

  • Just wondering...  does the class definition for PartFlights need the XmlRoot attribute?
    The question might get better response on an XML forum.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 04, 2013 6:04 PM
  • Unfortunately it's not working, I have added XmlRoot attribute to PartFlights.
    Monday, April 08, 2013 6:51 AM
  • I moved this to an XML forum.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 08, 2013 4:37 PM
  • Change your classes as follows:

        public class PartFlights
        {
            [XmlElement("Flight", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Flight { get; set; }
    
            [XmlElement("Origin", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Origin { get; set; }
    
            [XmlElement("Status", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Status { get; set; }
    
            [XmlElement("Time", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public string Time { get; set; }
        }
    
        [XmlRoot("ArrayOfPartFlights", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
        public class Flights
        {
            [XmlElement("PartFlights", Namespace = "http://schemas.datacontract.org/2004/07/SaksoonAirport")]
            public PartFlights[] PartFlightsList { get; set; }
        }

    then create the XmlSerializer as

    XmlSerializer ser = new XmlSerializer(typeof(Flights));

    Or don't use a namespace in the serialization attributes and use

        public class PartFlights
        {
            [XmlElement("Flight")]
            public string Flight { get; set; }
    
            [XmlElement("Origin")]
            public string Origin { get; set; }
    
            [XmlElement("Status")]
            public string Status { get; set; }
    
            [XmlElement("Time")]
            public string Time { get; set; }
        }
    
        [XmlRoot("ArrayOfPartFlights")]
        public class Flights
        {
            [XmlElement("PartFlights")]
            public PartFlights[] PartFlightsList { get; set; }
        }

    then set up the XmlSerializer as

    XmlSerializer ser = new XmlSerializer(typeof(Flights), "http://schemas.datacontract.org/2004/07/SaksoonAirport");


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Marked as answer by Xyroid Tuesday, April 09, 2013 5:01 AM
    Monday, April 08, 2013 5:01 PM
  • It worked like a magic. Thanks Matt & Martin.
    Tuesday, April 09, 2013 5:01 AM