Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Serialize an object with a list of objects in it

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>
like image 779
Max Young Avatar asked Nov 15 '13 02:11

Max Young


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

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>
like image 150
Chris Sinclair Avatar answered Oct 31 '22 02:10

Chris Sinclair