简体   繁体   中英

Serialize Nested Classes in c#?

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:)

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.

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

This line is invalid:

[XmlElement("House number")] 

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

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM