Click here to Skip to main content
15,115,503 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# datetime compared to sql server datetime Pin
jschell28-Feb-13 11:08
Memberjschell28-Feb-13 11:08 
QuestionList view Question Pin
Member 979920426-Feb-13 15:57
MemberMember 979920426-Feb-13 15:57 
AnswerRe: List view Question Pin
Richard MacCutchan26-Feb-13 23:31
mveRichard MacCutchan26-Feb-13 23:31 
AnswerRe: List view Question Pin
Amir Mohammad Nasrollahi29-Jul-13 22:29
professionalAmir Mohammad Nasrollahi29-Jul-13 22:29 
QuestionHow do I populate child elements in XML? Pin
Magela26-Feb-13 13:56
MemberMagela26-Feb-13 13:56 
AnswerRe: How do I populate child elements in XML? Pin
Mangal Deep Gupta27-Feb-13 0:24
MemberMangal Deep Gupta27-Feb-13 0:24 
GeneralRe: How do I populate child elements in XML? Pin
Magela27-Feb-13 5:26
MemberMagela27-Feb-13 5:26 
AnswerRe: How do I populate child elements in XML? Pin
Jegan Thiyagesan27-Feb-13 5:31
MemberJegan Thiyagesan27-Feb-13 5:31 
Hi,
When it come to xml serialization and deserialization, I would always use them as objects.

C#
using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

namespace Test
{
    [Serializable]
    [XmlRoot("RootElement")]
    public class RootElement
    {
        [XmlElement]
        public int Id { get; set; }
        public string Name { get; set; }
        public Address Address_1 { get; set; }
        public Address Address_2 { get; set; }
    }

    public class Address
    {
        [XmlElement]
        public string Number {get; set;}
        public string Street { get; set; }
        public string Town { get; set; }
    }

    public class Test2
    {
        Address address_1 = new Address()
        {
            Number = "123",
            Street = "My Road",
            Town = "My Home Town"
        };

        Address address_2 = new Address()
        {
            Number = "PO BOX 456",
            Street = "My Street",
            Town = "My Birth Town"
        };

        public void setXmlValues()
        {
            RootElement testOut = new RootElement {
                                                    Id = 1,
                                                    Name = "Customer Name",
                                                    Address_1 = address_1,
                                                    Address_2 = address_2
                                                    };

            xml_serialise(testOut);

            RootElement testIn = xml_deserialise();
            int id = testIn.Id;
            string Name = testIn.Name;
            string myBirthTown = testIn.Address_2.Town;

        }


        private void xml_serialise(RootElement test)
        {
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));


            using (TextWriter writer = new StreamWriter("test.xml"))
            {
                ser.Serialize(writer, test);
            }
        }

        private RootElement xml_deserialise()
        {
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));

            RootElement test;

            using (TextReader writer = new StreamReader("test.xml"))
            {
                test = (RootElement)ser.Deserialize(writer);
            }

            return test;
        }
    }
}


I hope this helps.

Regards
Jegan
Think! Don't write a line of code unless you absolutely need to.

GeneralRe: How do I populate child elements in XML? Pin
Magela27-Feb-13 5:38
MemberMagela27-Feb-13 5:38 
QuestionOffice Development problems Pin
Daytona67526-Feb-13 12:32
MemberDaytona67526-Feb-13 12:32 
AnswerRe: Office Development problems Pin
Matt T Heffron26-Feb-13 12:58
professionalMatt T Heffron26-Feb-13 12:58 
AnswerRe: Office Development problems Pin
Jegan Thiyagesan27-Feb-13 6:04
MemberJegan Thiyagesan27-Feb-13 6:04 
GeneralRe: Office Development problems Pin
Matt T Heffron27-Feb-13 10:32
professionalMatt T Heffron27-Feb-13 10:32 
GeneralRe: Office Development problems Pin
Jegan Thiyagesan27-Feb-13 12:57
MemberJegan Thiyagesan27-Feb-13 12:57 
QuestionTab Delimited file to sql database Pin
gavindon26-Feb-13 11:03
Membergavindon26-Feb-13 11:03 
AnswerRe: Tab Delimited file to sql database Pin
Matt T Heffron26-Feb-13 11:27
professionalMatt T Heffron26-Feb-13 11:27 
GeneralRe: Tab Delimited file to sql database Pin
gavindon26-Feb-13 12:25
Membergavindon26-Feb-13 12:25 
QuestionListbox colours. Pin
Septimus Hedgehog26-Feb-13 8:35
MemberSeptimus Hedgehog26-Feb-13 8:35 
AnswerRe: Listbox colours. Pin
Alan N26-Feb-13 9:40
MemberAlan N26-Feb-13 9:40 
AnswerRe: Listbox colours. Pin
Dave Kreskowiak26-Feb-13 10:05
mveDave Kreskowiak26-Feb-13 10:05 
GeneralRe: Listbox colours. Pin
Septimus Hedgehog26-Feb-13 23:59
MemberSeptimus Hedgehog26-Feb-13 23:59 
GeneralRe: Listbox colours. Pin
Dave Kreskowiak27-Feb-13 2:01
mveDave Kreskowiak27-Feb-13 2:01 
GeneralRe: Listbox colours. Pin
Septimus Hedgehog27-Feb-13 6:39
MemberSeptimus Hedgehog27-Feb-13 6:39 
Questioncan floats be multiplied and come up with a fraction? Pin
notahack26-Feb-13 7:01
Membernotahack26-Feb-13 7:01 
AnswerRe: can floats be multiplied and come up with a fraction? Pin
Richard Deeming26-Feb-13 7:15
mveRichard Deeming26-Feb-13 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.