Thank you very much for the quick answer.
That seems to be another way to replace the list of objects with a list of ID references. I can definitely do that and add a method in the class to return the actual objects from the IDs. However, that means for every type I define, I need to refer with ID and then make a method to turn that back into the object.
I wonder whether there are more "automatic" ways to implement it. I want to make my code shorter and easier to read. I did find some hint from the stackoverflow:
https://stackoverflow.com/questions/1617528/net-xml-serialization-storing-reference-instead-of-object-copy
However, as a beginner of C#, I am having a hard time to turn the single object example into a list/array of object example. If you can help me a bit, it will be greatly appreciated.
Here is how I implemented with the second method:
using ExtendedXmlSerializer;
using ExtendedXmlSerializer.Configuration;
using System.IO;
IExtendedXmlSerializer serializer = new ConfigurationContainer().ConfigureType<Character>()
.EnableReferences(p => p.ID)
.Create();
string xml = serializer.Serialize(new XmlWriterSettings { Indent = true }, characters);
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//test.xml";
File.WriteAllText(path, xml);
It gave me this
<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ns1="clr-namespace:Serialization;assembly=Serialization" xmlns:exs="https://extendedxmlserializer.github.io/v2" exs:arguments="ns1:Character" xmlns="https://extendedxmlserializer.github.io/system">
<Capacity>4</Capacity>
<ns1:Character ID="0">
<Name>A</Name>
<Friends>
<Capacity>4</Capacity>
<ns1:Character ID="1">
<Name>B</Name>
</ns1:Character>
<ns1:Character ID="2">
<Name>C</Name>
</ns1:Character>
</Friends>
</ns1:Character>
<ns1:Character exs:entity="1" />
</List>
which does not seem like what I want because I failed to deserialize it:
loadedCharacters = serializer.Deserialize<List<Character>>(xml);