Renaming array items in a flat xml array using an XmlSerializer?

Use a wrapper class that contains the array, this will allow you to apply the XmlElement attribute.

Use a wrapper class that contains the array, this will allow you to apply the XmlElement attribute: public class MyClassList { XmlElement("Item") public MyClass Items { get; set; } } var items = new { new MyClass { Property1 = "A", Property2 = "B" }, new MyClass { Property1 = "C", Property2 = "D" }, }; var list = new MyClassList { Items = items }; using (var writer = new StringWriter()) { var xs = new XmlSerializer(typeof(MyClassList), new XmlRootAttribute("Items")); xs. Serialize(writer, list); writer.ToString().Dump(); }.

Thanks! This works perfectly. – xmlWiz Dec 12 '10 at 12:27 I'm not so sure why it works though :-) I was running around after XmlArrayItem and XmlAttributeOverrides.

Thanks again! – xmlWiz Dec 12 '10 at 12:30.

Personally I would serialize and deserialize manually - I've found that it's easier to get whatever flexibility you want that way rather than spending a long time messing around with the built-in serialization and living with the various restrictions it imposes. LINQ to XML makes it pretty simple. For example, in this case: XDocument doc = XDocument.

Load("test. Xml"); // You could use an array if you really wanted, of course. List list = doc.

Root . Elements("Item") . Select(x => new MyClass { Property1 = (string) x.

Attribute("Property1"), Property2 = (string) x. Attribute("Property2"), }) .ToList(); Admittedly this will get hairy if you need to serialize objects with complicated relationships (two objects referring to the same child or whatever) but I've had a great deal of success with it in the past.

Thanks for the tip! In my case, the actual MyClass structure is pretty complex, with nested hierarchies - so this approach could get a bit complicated... – xmlWiz Dec 12 '10 at 12:33 @xmlWiz: If you follow a pattern with each class knowing how to deserialize and serialize itself, I've found it's not too bad - and easy to unit test. But if you have shared references etc, it does get complicated.

– Jon Skeet Dec 12 '10 at 12:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions