First of all, its not nice to ask about your problem with some object tokens without showing the definition of its type. It's also not nice to show the code which does not compile. What, was the using a fragment of real code a problem? How about copy/paste?
However, it does not matter it you try to use the "solution" like in the code you show. Let's understand why it cannot work. Essentially,
sw.Write(tokens);
will output
sw.Write(tokens.ToString());
For lists, other containers and most other types it will simply output the string containing the type name; so, what's the use?
You need to use some kind of
serialization, and this is something you will probably need to learn. Please start here:
http://en.wikipedia.org/wiki/Serialization[
^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization%28v=vs.100%29.aspx[
^].
I would strongly recommend to use
Data Contract and
DataContractSerializer
. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[
^].
This approach is easiest to use, very robust, universal and non-intrusive. I advocate this approach in my past answers:
How can I utilize XML File streamwriter and reader in my form application?[
^],
Creating property files...[
^].
—SA