jullin
jullin

Reputation: 633

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>

Upvotes: 4

Views: 7192

Answers (6)

Uriel Silva
Uriel Silva

Reputation: 38

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;
        }

Upvotes: 0

krystan honour
krystan honour

Reputation: 6793

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; }
    }
}

Upvotes: 1

Jason
Jason

Reputation: 1969

[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.

Upvotes: 1

Security Hound
Security Hound

Reputation: 2551

bool variables can either be true or false which have an integer value of 1 and 0. So your XML has invalid data and/or your class properties are of the wrong type.

Upvotes: 1

Pharabus
Pharabus

Reputation: 6062

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)

Upvotes: 1

SLaks
SLaks

Reputation: 887453

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

Upvotes: 8

Related Questions