Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load("C:\\Person.xml");
            string strXML = doc.InnerXml;// Read XML in string
            //now set this xml to object
            Person objPerson = new Person();
            objPerson.SetXML(strXML);
            string str = "coding done!";
        }
    }

    [Serializable]
    public class Person
    {
        public Person()
        {
        }

        [XmlElement(ElementName = "Name")]
        public string Name
        {
            get;
            set;
        }
        [XmlElement(ElementName = "BirthDate")]
        public PersonDate BirthDate
        {
            get;
            set;
        }
        [XmlElement(ElementName = "Account")]
        public PersonAccount Account
        {
            get;
            set;
        }

        public String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            constructedString = constructedString.Remove(0, 1);
            return (constructedString);
        }

        public object DeSerializeObject(string r_strXML, System.Type r_objType)
        {
            StringReader strReader = new StringReader(r_strXML);
            XmlSerializer mySerializer = new XmlSerializer(r_objType);           
            return (object)mySerializer.Deserialize(strReader);
        }
                       
        public string SerializeObject(object r_objObject, System.Type r_objType)
        {
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(r_objType);
            System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, r_objObject);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(XmlizedString);
            doc.DocumentElement.RemoveAttribute("xmlns:xsd");
            doc.DocumentElement.RemoveAttribute("xmlns:xsi");
            XmlizedString = doc.OuterXml;
            return XmlizedString;
        }

        public string GetXML()
        {
            return SerializeObject(this, this.GetType());
        }

        public  void SetXML(string r_strXML)
        {
            Person objPerson;
            objPerson = (Person)DeSerializeObject(r_strXML, typeof(Person));
            this.Name = objPerson.Name;
            this.BirthDate = objPerson.BirthDate;
            this.Account = objPerson.Account; 
        }

    }
    [Serializable]
    public class PersonDate
    {
        public PersonDate()
        {
        }

        [XmlAttribute("Day")] 
        public string Day
        {
            get;set;
        }
        [XmlAttribute("Month")] 
        public string Month
        {
            get;
            set;
        }
        [XmlAttribute("Year")] 
        public string Year
        {
            get;
            set;
        }
    }
    [Serializable]
    public class PersonAccount
    {
        public PersonAccount()
        {
        }

        [XmlElement(ElementName = "AccountNumber")]
        public int AccountNumber
        {
            get;
            set;
        }
        [XmlElement(ElementName = "AccountType")]
        public string AccountType
        {
            get;
            set;
        }
    }

    /*Person.xml
     *         <Person>
            <Name>Sonu</Name>
            <BirthDate Day="14" Month="04" Year="1987"/>
            <Account>
                <AccountNumber>1234</AccountNumber>
                <AccountType>Savings</AccountType>
            </Account>
        </Person>
     * 
     * 
     * */


}
Posted
Comments
Sandeep Mewara 9-Jul-12 11:48am    
Not clear. What is the issue here? Where are you stuck? Please be specific.
Matt T Heffron 9-Jul-12 14:25pm    
So, when you single-step the code, where does it stop doing what you expect? (You have single-stepped the code? Right?)
Sergey Alexandrovich Kryukov 9-Jul-12 15:36pm    
Well, System.Xml.XmlDocument is Object. String is Object. Everything is System.Object. I think I do know what do you really want (at least I can guess), but you apparently don't, so what's the use of the answer? :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900