Serialize Nested Classes in c#?

19,413

Solution 1

What you really want serialize is this :

    Person p = new Person();
    p.Name = _name;
    p.Age = _age;
    Adress a = new Adress();

But these variables are local. Create a property of each one and decorate them with the serializable attribute too. Now it will work.

public SampleClass(string _name, byte _age, string _street, int _number)
{
    this.Person = new Person();
    Person p = this.Person;
    p.Name = _name;
    p.Age = _age;
    this.Adress = new Adress();
    Adress a = this.Adress;
    a.Street = _street;
    a.Number = _number;
}

[Serializable]
public Person Person { get; set; }
[Serializable]
public Adress Adress { get; set; }

BTW: Address takes 2 d.

Solution 2

If you serialize an instance of the main class, the serializer will serialize an instance of the nested class if and only if the object graph contains one. In this respect, nested classes are exactly the same as all other classes.

Basically you have to create properties for the nested class in the main one

Solution 3

This line is invalid:

[XmlElement("House number")] 

As an element name can't have a space in it.

Solution 4

The reason you are getting an empty XML file is that your SampleClass has no properties to serialize.

In the constructor you are creating a Person and Address which are thrown away as soon as the method exists as you are not using them for anything. Change your code as follows and you should have more success.

[Serializable]
public class SampleClass
{
    [Serializable]
    public class Person
    {
        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Age")]
        public ushort Age { get; set; }
    }

    [Serializable]
    public class Adress 
    {
        [XmlElement("Street")]
        public string Street { get; set; }

        [XmlElement("HouseNumber")]
        public int Number { get; set; }
    }

    public SampleClass()
    { 
    }

    public SampleClass(string name, byte age, string street, int number)
    {
        this.Person = new Person
        {
            Age = age,
            Name = name    
        };

        this.Adress = new Adress
        {
            Street = street,
            Number = number
        }
    }

    public Person Person { get; set; }
    public Address Address { get; set; }
}
Share:
19,413
Harry89pl
Author by

Harry89pl

C#, ASP.NET MVC/ few years ago webforms, Silverlight, WPF, Winforms, XNA 4.0, Ext.Net controls, jQuery, WCF, Windows Phone 7.5 Check my personal token: https://personaltokens.io/CHAR Check my blog in polish: http://charubiniak.pl

Updated on June 04, 2022

Comments

  • Harry89pl
    Harry89pl almost 2 years

    What I'm trying to do is serialize Nested classes. My code first:

    [Serializable]
    public class SampleClass
    {
        [Serializable]
        public class Person
        {
            [XmlElement("Name")]
            public string Name { get; set; }
            [XmlElement("Age")]
            public ushort Age { get; set; }
        }
        [Serializable]
        public class Adress 
        {
            [XmlElement("Street")]
            public string Street { get; set; }
            [XmlElement("House number")]
            public int Number { get; set; }
        }
        public SampleClass()
        { 
    
        }
        public SampleClass(string _name, byte _age, string _street, int _number)
        {
            Person p = new Person();
            p.Name = _name;
            p.Age = _age;
            Adress a = new Adress();
            a.Street = _street;
            a.Number = _number;
        }
    }
    

    I want to get xml like this

    <?xml version="1.0"?>
    <SampleClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <Person>
        <Name></Name>
        <Age></Age>
    </Person>
    <Adress>
        <Street></Street>
        <HouseNumber></HouseNumber>
    </Adress>
    </SampleClass>
    

    When I serialize this SimleClass:

    using (Stream str = new FileStream(@"C:/test.xml", FileMode.Create))
                {
                    XmlSerializer serial = new XmlSerializer(typeof(SampleClass));
                    SampleClass sClass = new SampleClass("John",15,"Street",34);
                    serial.Serialize(str, sClass);
                    label1.ForeColor = Color.Black;
                    label1.Text = "Ok";
                }
    

    It's give me test.xml file but inside of that file is :

    <?xml version="1.0"?>
     <SampleClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
    

    What am I doing wrong?

    Thanks for advance:)