最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Xml Deserializer not giving the expected output result - Stack Overflow

programmeradmin0浏览0评论

I am trying to understand Xml Serialization/Deserialized in C#. I have a Xml file. But when I tried to deserialize it, I am not getting the object value correctly. The debugger stops at below line without any errors. Is there anything missing in my code?

XmlSerializer deserializer = new XmlSerializer(typeof(Person));

Code:

    public class Program
    {
        //SOAP, the Simple Object Access Protocol, is an XML format used for the 
         //exchange of structured 
        //information,
        //here we serialize object using this Format
        static void Main(string[] args)
        {
            //Person a = new Person("adam","us");
            ////create a stream to write to a file 
            //Stream ab = File.OpenWrite("person.xml");
            //// create the XMLSerializer 
            //XmlSerializer serializer = new XmlSerializer(typeof(Person));
            //// serialize the object 
            //serializer.Serialize(ab, a);

            //ab.Close();
            Stream inputStream = File.OpenRead("person.xml");
        
            XmlSerializer deserializer = new XmlSerializer(typeof(Person));
            Person DeSerializedPerson =(Person)deserializer.Deserialize(inputStream);
            foreach(Person person in DeSerializedPerson)
            {
                Console.WriteLine(person.Name);
                Console.WriteLine(person.City);
            }
        }
    }
    public class Person: IEnumerable<Person>
    { 
        private string name;
        private string city;

        Person p;
        public Person()
        {
            // do nothing 
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public string City
        {
            get { return this.city; }
            set { this.city = value; }
        }
        public Person(string intnamePram,string cityParam)
        {
            this.name = intnamePram;
            this.city = cityParam;
        }
        //public IEnumerator<Person> GetEnumerator()
        //{
        //  return p.GetEnumerator();
        //}
        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //  return ContactList.GetEnumerator();
        //}
        public IEnumerator<Person> GetEnumerator()
        {
            return this.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public void Add(Person value)
        {
            this.p = value;
        }
    }
发布评论

评论列表(0)

  1. 暂无评论