7

In C# if I serialize an object that has a list of objects in it will it also serialize the list?

Example

public class Move {
    public string MoveName {get;  set;}

    public List<Tag> oTags = new List<Tag>;
}

public class Tag {
    public string TagName {get; set;}
}

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?

<Move>
  <MoveName>name</MoveName>
  <Tag>Value</Tag>
  ...
</Move>
Max Young
  • 1,522
  • 1
  • 16
  • 42
  • 1
    Regardless of the current answers, _how_ are you serializing it? For example, if you use the `XmlSerializer`, it will work. (though you may have to use some special attributes or creativity to get the specific XML schema output you want) – Chris Sinclair Nov 15 '13 at 02:49
  • I am using XmlSerializer – Max Young Nov 15 '13 at 02:50
  • 1
    How do you want the schema? It seems a bit ambiguous how you handle many tags. Should it be `nameValue1Value2...ValueN`? Also, are you flexible with changing your `Move` and `Tag` classes? – Chris Sinclair Nov 15 '13 at 02:53
  • Well I was simplifying the setup because really I was just looking for an answer to whether serialization would dig through my list in my object which is the desired effect in my case. – Max Young Nov 15 '13 at 02:57
  • Additionally, check if object has added in the list (possible bug?!) – antonio Mar 16 '17 at 05:50

3 Answers3

10

Yes, using the XmlSerializer it will serialize a List<T> so long as T (or in your case Tag) is serializable.

Move move = new Move { MoveName = "MyName" };
move.oTags.Add(new Tag { TagName = "Value1" } );
move.oTags.Add(new Tag { TagName = "Value2" } );
move.oTags.Add(new Tag { TagName = "Value3" } );

StringBuilder output = new StringBuilder();
var writer = new StringWriter(output);

XmlSerializer serializer = new XmlSerializer(typeof(Move));
serializer.Serialize(writer, move);

Console.WriteLine(output.ToString());

This outputs using your current class structure as:

<?xml version="1.0" encoding="utf-16"?>
<Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <oTags>
    <Tag>
      <TagName>Value1</TagName>
    </Tag>
    <Tag>
      <TagName>Value2</TagName>
    </Tag>
    <Tag>
      <TagName>Value3</TagName>
    </Tag>
  </oTags>
  <MoveName>MyName</MoveName>
</Move>

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:

public class Move 
{
    [XmlElement(Order = 1)]
    public string MoveName {get; set;}

    [XmlElement(Order = 2, ElementName = "Tags")]
    public List<Tag> oTags = new List<Tag>();
}

public class Tag 
{
    [XmlText]
    public string TagName {get; set;}
}

Which when serialized will produce:

<?xml version="1.0" encoding="utf-16"?>
<Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MoveName>MyName</MoveName>
  <Tags>Value1</Tags>
  <Tags>Value2</Tags>
  <Tags>Value3</Tags>
</Move>
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • Although that doesn't match what I am looking for scheme wise it does answer my question of whether it will dig through the list in my object I am serializing. The link Brendan Hill posted gives me a pretty good idea how to set it up for the layout I want. Thank you. – Max Young Nov 15 '13 at 03:07
  • @maxinfet: Check my edit/update, I found a set of attributes that will produce an output that matches your schema. – Chris Sinclair Nov 15 '13 at 03:10
  • @maxinfet: Also, I botched my serialization code slightly, so maybe re-check it if you had already copy/pasted it. – Chris Sinclair Nov 15 '13 at 03:14
  • can you check this problem too if possible ty : http://stackoverflow.com/questions/42490161/how-to-serialize-listlistobject – Furkan Gözükara Feb 27 '17 at 16:02
3

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

 XmlSerializer serializer = new XmlSerializer(typeof(YourClass)); 

In Your case

Move m = new Move();
            m.oTags.Add(new Tag() { TagName = "X" });
            m.oTags.Add(new Tag() { TagName = "XX" });

            XmlSerializer x = new XmlSerializer(typeof(Move));
            System.IO.Stream s;

            var xmlwriter = System.Xml.XmlWriter.Create("C:\\MXL.txt"); 
            x.Serialize(xmlwriter, m);

OutPut

    <?xml version="1.0" encoding="utf-8"?>
    <Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <oTags>
<Tag>
  <TagName>X</TagName>
</Tag>
<Tag>
   <TagName>XX</TagName>
</Tag>
</oTags></Move>
Kas
  • 3,747
  • 5
  • 29
  • 56
1

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:

XML Serialize generic list of serializable objects

Community
  • 1
  • 1
Brendan Hill
  • 3,406
  • 4
  • 32
  • 61