0

I'm creating an object which will be serialised and verified against an XSD to create an XML file. The problem I'm having is certain nodes in the XML are duplicated but with different names, so far i've got the following:

    public class export
    {
        public info info { get; set; }
        public design design { get; set; }
    }

    public class info
    {
        public DateTime exportDateTime { get; set; }
        public string duration { get; set; }
        public planningSoftware planningSoftware { get; set; }
        public exporter exporter { get; set; }
    }

    public class planningSoftware
    {
        public string name { get; set; }
        public string major { get; set; }
        public string minor { get; set; }
        public string revision { get; set; }
        public string build { get; set; }
    }

    public class exporter
    {
        public version version { get; set; }
        public module module { get; set; }
    }

    public class version
    {
        public string name { get; set; }
        public string major { get; set; }
        public string minor { get; set; }
        public string revision { get; set; }
        public string build { get; set; }
    }

    public class module
    {
        public string name { get; set; }
        public string major { get; set; }
        public string minor { get; set; }
        public string revision { get; set; }
        public string build { get; set; }
    }

    public static void buildXML()
    {
        var export = new export()
        {
            info = new info()
            {
                exportDateTime = DateTime.Today,
                duration = 10,
                planningSoftware = new planningSoftware()
                {
                    name = "Planning App",
                    major = "10",
                    minor = "4",
                    revision = "6",
                    build = "225"
                },
                exporter = new exporter()
                {
                    version = new version()
                    {
                        name = "App Test",
                        major = "10",
                        minor = "1",
                        revision = "0",
                        build = "201"

                    },
                    module = new module()
                    {
                        name = "App Test Module",
                        major = "5",
                        minor = "2",
                        revision = "1",
                        build = "220"
                    }
                }
            }
        };

        var s = new XmlSerializer(typeof(export));
        using (var f = File.Open("export.xml", FileMode.Create))
        {
            s.Serialize(f, export);
        }

    }

My question is, how can i minimise the duplication of fields here? I might normally initialise a seperate object that holds the values but it looks like this will affect the serialisation.

Thanks.

Dan Hall
  • 1,474
  • 2
  • 18
  • 43
  • I don't believe so, I'm trying to serialise here and have an object that's duplicating the name, major, minor. revision and build properties, i need to minimise the duplication without affecting serialisation. – Dan Hall May 13 '16 at 14:34
  • planningSoftware and version have same properties so you can create a common base class for the two classes, then inherit the base class. – jdweng May 13 '16 at 16:30

1 Answers1

2

The names of the properties in the xml are based by default in the name of the properties, not the name of their types.

So simply create a single type for holding name and version:

public class VersionedName
{
    public string name { get; set; }
    public string major { get; set; }
    public string minor { get; set; }
    public string revision { get; set; }
    public string build { get; set; }
}

Then in your other types declare your properties of that type:

public class Export
{
    public Info info { get; set; }
}

public class Info
{
    public DateTime exportDateTime { get; set; }
    public int duration { get; set; }
    public VersionedName planningSoftware { get; set; }
    public Exporter exporter { get; set; }
}

public class Exporter
{
    public VersionedName version { get; set; }
    public VersionedName module { get; set; }
}

You can verify that the following produces the same XML output than your initial code:

var export = new Export()
{
    info = new Info()
    {
        exportDateTime = DateTime.Today,
        duration = 10,
        planningSoftware = new VersionedName()
        {
            name = "Planning App",
            major = "10",
            minor = "4",
            revision = "6",
            build = "225"
        },
        exporter = new Exporter()
        {
            version = new VersionedName()
            {
                name = "App Test",
                major = "10",
                minor = "1",
                revision = "0",
                build = "201"

            },
            module = new VersionedName()
            {
                name = "App Test Module",
                major = "5",
                minor = "2",
                revision = "1",
                build = "220"
            }
        }
    }
};

var s = new XmlSerializer(typeof(Export));
using (var f = File.Open("export.xml", FileMode.Create))
{
    s.Serialize(f, export);
}
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112