Serialization newbie!

OK - new to this so please feel free to give advice! Here is my code so far:

public class GroupInfo
	private List<Structure> _sectors = new List<Structure>();
	public List<Structure> Sectors
	{ get; set; }

	public List<Users> Person
	{ get; set; }

	static public void SerializeToXML(GroupInfo groupInfoXML)
		XmlSerializer serializer = new XmlSerializer(typeof(GroupInfo ));
		TextWriter textWriter = new StreamWriter(@"C:\\xmltest.xml");
		serializer.Serialize(textWriter, groupInfoXML);

	static public void Main()
		GroupInfo groupInfoXML = new GroupInfo();
		groupInfoXML.Sectors = Structure.GetSectors();
		groupInfoXML.Person = Users.GetTeam();


Obviously (i think!) other classes are used in this code to bring back lists of secotrs and users. This works fine so far but…

I want the people to sit within the Sector Node - currently they are independant.
I need to loop through each sector and add the team members for that sector (the GetTeam method is overloaded to accept a sectorID)

How do I do these 2 things - please feel free to give adive or one or the other or any other part of my code they may be improved.


Make your List<People> a property of the SectorNode class and that will happen automatically. Depending on how you want the XML to read, you can use a few different attributes to handle it.

Sorry, not quite sure what you mean :blush:

The xml needs to be

<sector> //sector 1
<person>person in sector 1
<person>persons in sector 1
<sector> ////sector 2
<person>person in sector 2
<person>person in sector 2


Sectors should have a persons property:

public class Sector {
    public Person[] People { get; set; }

What is the root element? That will need to be a class that have a Sectors property setup similarly to the above.

The root element is <Info> - this contains multiple sectors which in tern conatains multitple people etc

:blush: Sorry - i’m really not getting this! Been trying for ages.

please could you show me the code for building and calling the class for people and sectors - I will then be able to pick it apart and build the rest (hopefully)!


Ok, here you go:

public class SectorInfo
	public Sector[] Sectors {get; set;}

public class Sector
	public Person[] People {get; set}

public class Person 
	public string FirstName {get; set;}
	public string LastName {get; set;}
	//other properties, etc, as required 
	//with whatever attributes you want to make them fit your schema

I’m using arrays rather than collections as that makes alot more sense on the wire, but you could use List<Person> and List<Section> if you want . . .

OK -thanks for that. I’m not sure i’m thinking of this in the correct way.

I have built a web based system to manage sectors and the teams, contacts priorities etc within. This system includes several classes with many different methods, such as the Structure class which include the GetStructure() method and the People class which include the GetPeople() method.

I was thinking that I could write a webservice which builds some xml to save to a file which used all my existing classes/methods rather than building new ones. I could call the GetStructure() method to return all Sectors and then loop through this, using the GetPeople() method using the Structure ID to return the People for each sector (and so on).

Is this correct? If that is the case, how do I call my web class to fill all the relevant bits of the xml structure - looping through the list of sectors to add all people for that sector etc?

I think I am getting there but still missing a bit of understanding over what I am doing!

Thanks for your time and patients! :slight_smile:

In that case, you might want to take a look at something like WCF or openrasta – they are .NET service stacks designed to make it easy to do just what you want.

One other word of advice – you want to keep service interfaces “chunky” as the expensive part of the operation is the remote call, not sending the data down the wire, in most cases.

Thanks for all your help. I’m under a bit of pressure to get this done, so i think i’m going to go down the xmldocument route as i think I have more knowledge on this, and then revisit when i get more time.

Thanks again. :slight_smile:

You really should consider using wwb’s classes (just for serialization) or use LINQ to XML. You can also use LINQ to populate wwb’s classes.

LINQ to XML could be as simple as

            var xdoc = new XDocument(
                new XElement("GroupInfo", 
                    Structure.GetSectors().Select(sector => new XElement("sector", 
                        Users.GetUsersForSector().Select(u => new XElement("person", 
                            new XAttribute("name", u.Name)))))));