简体   繁体   中英

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>

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

as SLaks says

also your Point object shows both fields as bools yet the values in the xml file are ints at least (21, 3,5,7 etc)

bool变量可以是true或false,其整数值为1和0.因此,您的XML具有无效数据和/或您的类属性的类型错误。

[XmlElement("Point")]
public List<Point> Items
{
  get { return items; }
  set { items = value; }
}

And in your point class both x and y should not be bools.

Solution:

namespace XmlStackProblem
{
    class Class1
    {

        public static void Main()
        {
            Points resultObject;

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

    [Serializable]
    [XmlRoot(IsNullable = false)]
    public class Points
    {
        [XmlElementAttribute("Point")]
        public List<Point> Point
        {
            get; set;
        }
    }

    [Serializable]
    [XmlType(AnonymousType = true)]
    public class Point
    {
        [XmlAttribute]
        public int x
        {
            get;
            set;
        }

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

You could using a DeSerialize function that return the object type like this example:

public T DeSerializeFromString<T>(string data)
        {
            T result;
            StringReader rdr = null;
            try
            {
                rdr = new StringReader(data);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                result = (T)xmlSerializer.Deserialize(rdr);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                rdr.Close();
            }
            return result;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM