How to serialize a nested class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a class like this:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class Person
{
<span style="color:Blue; int id;

<span style="color:Blue; public <span style="color:Blue; int Id
{
<span style="color:Blue; get { <span style="color:Blue; return id; }
<span style="color:Blue; set { id = value; }
}

<span style="color:Blue; string name;

<span style="color:Blue; public <span style="color:Blue; string Name
{
<span style="color:Blue; get { <span style="color:Blue; return name; }
<span style="color:Blue; set { name = value; }
}

<span style="color:Blue; int age;

<span style="color:Blue; public <span style="color:Blue; int Age
{
<span style="color:Blue; get { <span style="color:Blue; return age; }
<span style="color:Blue; set { age = value; }
}

<span style="color:Blue; bool sex;

<span style="color:Blue; public <span style="color:Blue; bool Sex
{
<span style="color:Blue; get { <span style="color:Blue; return sex; }
<span style="color:Blue; set { sex = value; }
}

Address address;

<span style="color:Blue; internal Address Address
{
<span style="color:Blue; get { <span style="color:Blue; return address; }
<span style="color:Blue; set { address = value; }
}

List<<span style="color:Blue; string> hobby;

<span style="color:Blue; public List<<span style="color:Blue; string> Hobby
{
<span style="color:Blue; get { <span style="color:Blue; return hobby; }
<span style="color:Blue; set { hobby = value; }
}
}

the Address Class like <span style="color:Blue; this:

<span style="color:Blue; class Address
{
<span style="color:Blue; string companyAddress;

<span style="color:Blue; public <span style="color:Blue; string CompanyAddress
{
<span style="color:Blue; get { <span style="color:Blue; return companyAddress; }
<span style="color:Blue; set { companyAddress = value; }
}

<span style="color:Blue; string homeAddress;

<span style="color:Blue; public <span style="color:Blue; string HomeAddress
{
<span style="color:Blue; get { <span style="color:Blue; return homeAddress; }
<span style="color:Blue; set { homeAddress = value; }
}
}
[/code]
I want to serialize a List<Person> to a xml file, what should I do?
I tried to add [XmlElement(ElementName="...")] before all of the properties of Person class and Address class, but after serializing, I got a xml file which all the properties I want to serialize as an element of the xml been serialized as attribute. What
was worse, the List<string> hobby and Address property cant be serialized.
Need help!

View the full article
 
Back
Top