Click here to Skip to main content
12,444,237 members (47,516 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: XML
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 8-Jul-12 19:40pm
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?)
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100