Answered by:
How to serialize a class with nested class

Question
-
I have a class like this:
class Person { int id; public int Id { get { return id; } set { id = value; } } string name; public string Name { get { return name; } set { name = value; } } int age; public int Age { get { return age; } set { age = value; } } bool sex; public bool Sex { get { return sex; } set { sex = value; } } Address address; internal Address Address { get { return address; } set { address = value; } } List<string> hobby; public List<string> Hobby { get { return hobby; } set { hobby = value; } } } the Address Class like this: class Address { string companyAddress; public string CompanyAddress { get { return companyAddress; } set { companyAddress = value; } } string homeAddress; public string HomeAddress { get { return homeAddress; } set { homeAddress = value; } } }
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 can't be serialized.
Need help!
Tuesday, November 22, 2011 4:24 PM
Answers
-
> 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 can't be serialized.
use "public Address Address" instead of "internal Address Address"
using System; using System.Collections.Generic; using System.Web.Script.Serialization; using System.IO; using System.Xml.Serialization; namespace ConsoleApplication1 { class Program { [STAThread] static void Main(string[] args) { var list = new List<Person> { new Person { Id = 1, Name = "p1", Age = 20, Sex = true, Address = new Address { CompanyAddress = "ca1", HomeAddress = "ha1" }, Hobby = new List<string> { "h1", "h2" } }, new Person { Id = 2, Name = "p2", Age = 20, Sex = true, Address = new Address { CompanyAddress = "ca2", HomeAddress = "ha2" }, Hobby = new List<string> { "h3", "h3" } }, }; // to xml var xs = new XmlSerializer(typeof(List<Person>)); var sw = new StringWriter(); xs.Serialize(sw, list); Console.WriteLine(sw.ToString()); // to json // 1) add references to System.Web.Extensions.dll; // 2) the Project Properties - Application - 'Target framework' should be '.Net Framework 4' var json = new JavaScriptSerializer().Serialize(list); Console.WriteLine("\n\n" + json); Console.ReadLine(); } } public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Sex { get; set; } public Address Address { get; set; } public List<string> Hobby { get; set; } } public class Address { public string CompanyAddress { get; set; } public string HomeAddress { get; set; } } }
- Marked as answer by Allen Li - Coding4Fun Sunday, November 27, 2011 3:14 AM
Tuesday, November 22, 2011 6:32 PM
All replies
-
Friend,
What you need to add is [Serializable()] attrubute for the class and the List property you have. The rest will be taken care by the framework.
Click here for more info.
-- Thanks Ajith R Nair- Edited by Ajith R Nair Tuesday, November 22, 2011 6:18 PM
Tuesday, November 22, 2011 6:17 PM -
> 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 can't be serialized.
use "public Address Address" instead of "internal Address Address"
using System; using System.Collections.Generic; using System.Web.Script.Serialization; using System.IO; using System.Xml.Serialization; namespace ConsoleApplication1 { class Program { [STAThread] static void Main(string[] args) { var list = new List<Person> { new Person { Id = 1, Name = "p1", Age = 20, Sex = true, Address = new Address { CompanyAddress = "ca1", HomeAddress = "ha1" }, Hobby = new List<string> { "h1", "h2" } }, new Person { Id = 2, Name = "p2", Age = 20, Sex = true, Address = new Address { CompanyAddress = "ca2", HomeAddress = "ha2" }, Hobby = new List<string> { "h3", "h3" } }, }; // to xml var xs = new XmlSerializer(typeof(List<Person>)); var sw = new StringWriter(); xs.Serialize(sw, list); Console.WriteLine(sw.ToString()); // to json // 1) add references to System.Web.Extensions.dll; // 2) the Project Properties - Application - 'Target framework' should be '.Net Framework 4' var json = new JavaScriptSerializer().Serialize(list); Console.WriteLine("\n\n" + json); Console.ReadLine(); } } public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Sex { get; set; } public Address Address { get; set; } public List<string> Hobby { get; set; } } public class Address { public string CompanyAddress { get; set; } public string HomeAddress { get; set; } } }
- Marked as answer by Allen Li - Coding4Fun Sunday, November 27, 2011 3:14 AM
Tuesday, November 22, 2011 6:32 PM -
> internal Address Address {...} [...] Address property can't be serialized.
for the non-public properties serialization use DataContractSerializer http://stackoverflow.com/q/420710
Tuesday, November 22, 2011 6:50 PM -
What you need to add is [Serializable()] attrubute for the class and the List property you have.
You cannot put a Serializable attribute on a property. Using it on the class is useless for XML serialization.Wednesday, November 30, 2011 1:50 PM