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> * * * */ }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)