Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml serialization c#

Can't understand what I am doing wrong, the result set is empty.
My code:

class Class1
    {

        public static object DeSerialize()
        {
            object resultObject;

            XmlSerializer serializer = new XmlSerializer(typeof(PointsContainer));
           using (TextReader textReader = new StreamReader(@"d:\point.xml"))
            {
                resultObject = serializer.Deserialize(textReader);
            }

            return resultObject;


        }
    }

    [Serializable]
    [XmlRoot("Points")]
    public class PointsContainer
    {
        [XmlElement("Point")]       
        private List<Point> items = new List<Point>();

        public List<Point> Items
        {
            get { return items; }
            set { items = value; }
        }


    }


    [Serializable]   
    public class Point
    {      
        [XmlAttribute]
        public bool x { get; set; }

        [XmlAttribute]
        public bool y { get; set; }
    }

Xml:

<Points>  
   <Point x="1" y="5"/>
   <Point x="21" y="3"/>
   <Point x="3" y="7"/>
</Points>
like image 818
jullin Avatar asked Dec 17 '22 20:12

jullin


1 Answers

Move the [XmlElement] attribute to the property.
XmlSerializer ignores private members.

like image 120
SLaks Avatar answered Jan 06 '23 05:01

SLaks