Xdocument and foreach

How would i do the following so it works?!:


List<Structure> sectors = Structure.GetSectors();

XDocument doc = new XDocument(
	new XDeclaration("1.0", "utf-8", "yes"),
	new XElement("Info",
		foreach(Structure sector in sectors){
			new XElement("Sector",
			      new XAttribute("SectorName", "Trowbridge")
			)
		}

	)
);

cheers

I would ditch that code and take a look at Xml serialization, it really is the only way to fly.

OK - thanks for the advice - luckily I am just starting this part of my poject and was nusure of the best way to go. Let me just out line what I am doing to check that your advice is correct:

-I am creating a webservice which will write some xml to a file everyday ready for uploading to our website (there will only be one file which is overwritten).
-The xml information will mostly come from database tables
-I which to validate the xml against a schema - preferably BEFORE it is written to the file, so if it fails, the auto ftp program will just upload the last known working file without the need to check anything.

If XMLSerialisation is still the way to go, can you point me in the direction of any good tutorials to get me strated?

cheers

tap, tap, tap - is this on? :slight_smile:

List<Structure> sectors = Structure.GetSectors();

XDocument doc = new XDocument(
	new XDeclaration("1.0", "utf-8", "yes"),
	new XElement("Info",
		sectors.Select(x => new XElement("Sector", new Attribute("SectorName", "Trowbridge"))
			)
		}

	

Really not too hard, and not worth writing examples for serialization. Trick is to build a class that “looks” like the object and run it through the System.Xml.XmlSerializer to generate the XML. The painful part is deserialization, on which I can write a book.

cheers ww! See you on her soon with any probs I get! Luckily I don’t need to deserialize! :wink: