Is it possible to deserialize XML into List?

You can encapsulate the list trivially: using System; using System.Collections. Generic; using System.Xml. Serialization; XmlRoot("user_list") public class UserList { public UserList() {Items = new List();} XmlElement("user") public List Items {get;set;} } public class User { XmlElement("id") public Int32 Id { get; set; } XmlElement("name") public String Name { get; set; } } static class Program { static void Main() { XmlSerializer ser= new XmlSerializer(typeof(UserList)); UserList list = new UserList(); list.Items.

Add(new User { Id = 1, Name = "abc"}); list.Items. Add(new User { Id = 2, Name = "def"}); list.Items. Add(new User { Id = 3, Name = "ghi"}); ser.

Serialize(Console. Out, list); } }.

2 Nice solution with the XmlElement("user") to avoid an extra level of elements. Looking at this, I thought for sure that it would have emitted a or node (if you did not have the XmlElement attribute), and then add nodes under that. But I tried it and it did not, thus emitting exactly what the question wanted.

– Jon Kragh Jul 26 '10 at 16:38.

Yes, it will serialize and deserialize a List. Just make sure you use the XmlArray attribute if in doubt. Serializable public class A { XmlArray public List strings; } This works with both Serialize() and Deserialize().

Not sure about List but Arrays are certainly do-able. And a little bit of magic makes it really easy to get to a List again. Public class UserHolder { XmlElement("list") public User Users { get; set; } XmlIgnore public List UserList { get { return new List(Users); } } }.

– Daniel Schaffer Mar 3 '09 at 20:53 @Daniel, AFAIK, no. You need to serialize and deserialize into some concrete object type. I do not believe that XML serialization natively supports collection classes as the start of a serialization.

I do not 100% know that though. – JaredPar Mar 3 '09 at 20:55.

Yes, it does deserialize to List. No need to keep it in an array and wrap/encapsulate it in a list. Public class UserHolder { private List users = null; public UserHolder() { } XmlElement("user") public List Users { get { return users; } set { users = value; } } } Deserializing code, XmlSerializer xs = new XmlSerializer(typeof(UserHolder)); UserHolder uh = (UserHolder)xSerializer.

Deserialize(new StringReader(str)).

I think I have found a better way. You don't have to put attributes into your classes. I've made two methods for serialization and deserialization which take generic list as parameter.

Take a look (it works for me): private void SerializeParams(XDocument doc, List paramList) { System.Xml.Serialization. XmlSerializer serializer = new System.Xml.Serialization. XmlSerializer(paramList.GetType()); System.Xml.

XmlWriter writer = doc.CreateWriter(); serializer. Serialize(writer, paramList); writer.Close(); } private List DeserializeParams(XDocument doc) { System.Xml.Serialization. XmlSerializer serializer = new System.Xml.Serialization.

XmlSerializer(typeof(List)); System.Xml. XmlReader reader = doc.CreateReader(); List result = (List)serializer. Deserialize(reader); reader.Close(); return result; } So you can serialize whatever list you want!

You don't need to specify the list type every time. List list = new List(); list. Add(new AssemblyBO()); list.

Add(new AssemblyBO() { DisplayName = "Try", Identifier = "243242" }); XDocument doc = new XDocument(); SerializeParams(doc, list); List newList = DeserializeParams(doc).

Yes, it will serialize and deserialize a List. Just make sure you use the XmlArray attribute if in doubt.

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