Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

serialize multiple properties of a class to nested xml elements?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy B - 07 Apr 2008 00:59 GMT
How do you serialize multiple class properties to a nested xml element? For
example, I have an element called "Venue" I want to serialize part of my
class to. The "Venue" element will have attributes and a nested element
called "Address". How would I do this using a simple example?
Marc Gravell - 07 Apr 2008 07:56 GMT
> How would I do this using a simple example?

The simplest approach would be to keep the object and xml
representations in sync. You can still promote properties to the parent
by using a facade property and marking it as XmlIgnore. More complex
examples involve proxies (see the dictionary example), or custom
IXmlSerializable.

Marc

using System;
using System.IO;
using System.Xml.Serialization;
static class Program
{
    static void Main()
    {
        Venue venue = new Venue
        {
            Name = "The place",
            Address = new Address
            {
                Line1 = "Foo", Line2 = "Bar", PostCode="Zip"
            }
        };

        XmlSerializer ser = new XmlSerializer(typeof(Venue));
        StringWriter writer = new StringWriter();
        ser.Serialize(writer, venue);
        string xml = writer.ToString();
    }
}
[Serializable]
public sealed class Venue
{
    [XmlAttribute]
    public string Name { get; set; }
    public Address Address { get; set; }
    // PostCode is a facade property
    [XmlIgnore]
    public string PostCode
    {
        get
        {
            return Address == null ? null : Address.PostCode;
        }
    }
}
[Serializable]
public sealed class Address
{
    [XmlAttribute]
    public string Line1 { get; set; }
    [XmlAttribute]
    public string Line2 { get; set; }
    [XmlAttribute]
    public string PostCode { get; set; }

}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.