none
Very specific serializing C# class to XML

    Question

  • I need to serialize my C# class to XML looking something like so:

    <request>
        <session>12345</session>
        <page>1</page>
        <elements_per_page>999</elements_per_page>
        <location>
           <zone>aaaa</zone>
           <region>bbbb</region>
           <coordinates>
               <lat>38.680632</lat>
               <lon>-96.5001</lon>
           </coordinates>
        </location>
    </request>

    What I don't want is 3 classes (request, location, coordinates), I just want 1 class with all changable attributes as root of that class and then added some serialization tags that would create that nested XML, is that at all possible?

    Let's start with the bare class:

    class request
    {
        public int session { get; set; }
        public int page { get; set; }
        public int elements_per_page { get; set; }
        public string zone { get; set; }
        public string region { get; set; }
        public decimal lat { get; set; }
        public decimal lon { get; set; }
    }

    How do I map them so the XML like I described is created? Thanks for your help good people :)
    Wednesday, August 28, 2013 12:12 PM

Answers

All replies