I have a List that I populate with ToDo objects and save to XML. This works perfectly. I have problems with deserializing XML back to the List of ToDo objects. I know that there is already few simi.
In C# if I serialize an object that has a list of objects in it will it also serialize the list?
Example
If I serialize move will all the tags stored in move get serialized as well? Also if it will not serialize the list how would I go about making it do that?
3 Answers
Yes, using the XmlSerializer
it will serialize a List<T>
so long as T
(or in your case Tag
) is serializable.
This outputs using your current class structure as:
I'll see if I can find a way to match your current XML schema, but you can look up how to apply XmlAttributes and play around with it yourself.
EDIT:
If you change your class declaration to use the following XmlAttributes, you will achieve the exact XML schema as in your example:
Which when serialized will produce:
Chris SinclairChris SinclairC Serialize List Of Objects List
Are you sure that your class Declarations are right in your Question ? you are just declaring Public Move
, It should be Public class Move
Try this code
In Your case
OutPut
KasKasBy default, no it won't, since the items within the list may not be serializable.
If they are, then you may find the following page userful:
Not the answer you're looking for? Browse other questions tagged c#xmlserialization or ask your own question.
I have this, and i want to serialize to xml both lists :
I used SerializeToXML method that i found in other tutorial but when i run it an error shows up
'Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.List' is less accessible than method 'AppPrueba.Form1.SerializeToXML(System.Collections.Generic.List)' C:Users722825DesktopSanti CosasAppPruebaAppPruebaForm1.cs 27 28 AppPrueba'
Thanks in advance if you can help me!
3 Answers
All the classes that you are trying to serialize to xml should be public. And for collection serialization you cannot use generics - either use untyped collections, like, ArrayList , or create a non-generic descendant class form List .
alexC# Serialize Object To File
alexagen
is private and SerializeToXML
is public static. You need agen
public
Your error says that the variabel List agen = new List(); needs to be public like this: public List agen = new List();