Write to file a List<type>

  • Thread starter Thread starter A.Rocha
  • Start date Start date
A

A.Rocha

Hi,

I need save to text file a List<type>, but i dont wont serialize.

//
List<mytype> myList = new List<mytype>();

anyone can help me?
 
List<> won't serialize. You need to write some code to manually serialize
it.

Something like :

MyList<T> : List<T>
{

#region Manual serialization
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
int i = 0;
info.AddValue("num", this.Count);
foreach (T item in this)
{
info.AddValue("item" + i.ToString(), item);
++i;
}
}

protected MyList(SerializationInfo info, StreamingContext context)
{
int num = info.GetInt32("num");
for (int i = 0; i < num; ++i)
{
T item = (T)info.GetValue("item" + i.ToString(), typeof(T));
base.Add(item);
}
}
#endregion Manual serialization
}

HTH,

Adam,
=========
 
I personally would not serialize the data but rather list it in a text file,
especially if the case assumes that the data can be edited by users outside
the application. The I/O functions would save and read chunks of data (the
List<T> records).

Some key points we need to know to give an appropriate answer:
- What kind of data does the list store? e.g. proprietary format, text or
binary data
- Do you want the output to be editable by you or your users outside your
program?
--
Stanimir Stoyanov
http://stoyanoff.info

Adam Benson said:
List<> won't serialize. You need to write some code to manually serialize
it.

Something like :

MyList<T> : List<T>
{

#region Manual serialization
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
int i = 0;
info.AddValue("num", this.Count);
foreach (T item in this)
{
info.AddValue("item" + i.ToString(), item);
++i;
}
}

protected MyList(SerializationInfo info, StreamingContext context)
{
int num = info.GetInt32("num");
for (int i = 0; i < num; ++i)
{
T item = (T)info.GetValue("item" + i.ToString(),
typeof(T));
base.Add(item);
}
}
#endregion Manual serialization
}

HTH,

Adam,
=========
 
Hi Stanimir ,

//This is my class:

public class Info
{
public long ID;
public string A;
public float B;
public float C;
public int D;
public float E;
public string F;
public float G;
public string H;
public short I;

public Info(){ }

public Info(long ID, string A, float B, float C, int D, float E,
string F, float G, string H, short I)
{
this.ID= ID;
this.A= A;
this.B= B;
this.C= C;
this.D= D;
this.E= E;
this.F= F;
this.G= G;
this.H= H;
this.I= I;
}
}

//this is my List<Info>
List<Info> aInfo= new List<Info>();

//i add several items to List
aInfo.Add(new cInfoPOS(ID, A, B, C, D, E, F, G, H, I));
....
....

Now i want to save this in text file. The output is only edited by the
program.

Thanks for any help.

--
Best regards,
A.Rocha
(Portugal)



Stanimir Stoyanov said:
I personally would not serialize the data but rather list it in a text
file, especially if the case assumes that the data can be edited by users
outside the application. The I/O functions would save and read chunks of
data (the List<T> records).

Some key points we need to know to give an appropriate answer:
- What kind of data does the list store? e.g. proprietary format, text or
binary data
- Do you want the output to be editable by you or your users outside your
program?
 
I need save to text file a List<type>, but i dont wont serialize.

The last part of that sentence is unclear. Did you misspell "want," and you
meant to say "I don't want to use serialization"? Or did you mean "it won't
serialize"?

If you don't want to use .NET's serialization then you're going to have to
write the values in your class manually, using either a BinaryWriter or a
StreamWriter. The choice will depend on whether you want all your data to be
in plain text or a more compact binary format.
 
Adam Benson said:
List<> won't serialize. You need to write some code to manually serialize
it.

Why wouldn't it serialize? List<T> is marked as Serializable. It should
serialize with no problem, on the condition that the class "T" is itself
marked as Serializable.

Another alternative is to use the XmlSerializer, which will produce a
nicely strutured XML text file from a List<Info>, given the "Info" class
that the OP has shown in a separate message.
 
Hi,

sorry for only now i tell you that, but i'm working on compact framework 3.5
on windows Mobile 6.0, that's why i can't marke it as Serializable.

Thanks.
 
A.Rocha skrev:
Hi,

sorry for only now i tell you that, but i'm working on compact framework
3.5 on windows Mobile 6.0, that's why i can't marke it as Serializable.
I have serialized several types of List<xxx> on compact framework
without any problems, but since it is a kind of structural data you have
to serialize/deserialize it as XML.

Use [XmlIgnore] on the line before the declaration of elements in your
class you don't want to serialize.

using System.Xml.Serialization;
....


private static void StoreObject(String file_name, object o)
{
if (o == null)
return;
String xml_file = String.Concat(Config.StorageDir, file_name);

Stream stream = null;
try
{
stream = File.Create(xml_file);
XmlSerializer xsz = new XmlSerializer(o.GetType());
xsz.Serialize(stream, o);
}
finally
{
if (stream != null)
stream.Close();
}
}
....
List<Customer> Customers;
....
StoreObject("Customers.xml", Customers);
 
Thanks a lot Bjorn,

--
Best regards,
A.Rocha
(Portugal)

Bjørn Brox said:
A.Rocha skrev:
Hi,

sorry for only now i tell you that, but i'm working on compact framework
3.5 on windows Mobile 6.0, that's why i can't marke it as Serializable.
I have serialized several types of List<xxx> on compact framework without
any problems, but since it is a kind of structural data you have to
serialize/deserialize it as XML.

Use [XmlIgnore] on the line before the declaration of elements in your
class you don't want to serialize.

using System.Xml.Serialization;
...


private static void StoreObject(String file_name, object o)
{
if (o == null)
return;
String xml_file = String.Concat(Config.StorageDir, file_name);

Stream stream = null;
try
{
stream = File.Create(xml_file);
XmlSerializer xsz = new XmlSerializer(o.GetType());
xsz.Serialize(stream, o);
}
finally
{
if (stream != null)
stream.Close();
}
}
...
List<Customer> Customers;
...
StoreObject("Customers.xml", Customers);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top