C Serialize List Of Objects

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.

  1. C Serialize List Of Objects List
  2. C# Serialize Object To File
  3. C Serialize List Of Objects 2017
Active3 years, 10 months ago
Deserialize

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?

Max Young
Max YoungMax Young
7971 gold badge8 silver badges31 bronze badges

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 Sinclair

C Serialize List Of Objects List

20.2k2 gold badges42 silver badges79 bronze badges

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

KasKas
2,3422 gold badges17 silver badges45 bronze badges
C# serialize object to xml

By 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:

Community
Brendan HillBrendan Hill

Not the answer you're looking for? Browse other questions tagged c#xmlserialization or ask your own question.

Active1 year, 7 months ago

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!

user2115574user2115574

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 .

alex

C# Serialize Object To File

alex
10.7k3 gold badges36 silver badges57 bronze badges

agen is private and SerializeToXML is public static. You need agen public

ListDimaDima
6,3244 gold badges19 silver badges40 bronze badges

Your error says that the variabel List agen = new List(); needs to be public like this: public List agen = new List();

andreasnicoandreasnico

C Serialize List Of Objects 2017

Not the answer you're looking for? Browse other questions tagged c#xmlserialization or ask your own question.