Xml序列化c#

前端之家收集整理的这篇文章主要介绍了Xml序列化c#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
无法理解我做错了什么,结果集是空的.
我的代码

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>

解决方法

将[XmlElement]属性移动到属性. XmlSerializer忽略私有成员.

猜你在找的XML相关文章