By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Write to file a List<type>

P: n/a
Hi,

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

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

anyone can help me?

--
Best regards,
A.Rocha
Nov 5 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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,
=========
"A.Rocha" <ar**********@ifthensoftware.comwrote in message
news:89**********************************@microsof t.com...
Hi,

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

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

anyone can help me?

--
Best regards,
A.Rocha

Nov 5 '08 #2

P: n/a
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<Trecords).

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" <Ad*********@NOSPAMMYSPAM.omnibus.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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,
=========
"A.Rocha" <ar**********@ifthensoftware.comwrote in message
news:89**********************************@microsof t.com...
>Hi,

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

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

anyone can help me?

--
Best regards,
A.Rocha

Nov 5 '08 #3

P: n/a
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<InfoaInfo= 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" <st******@REMOVETHIS.live.comescreveu na mensagem
news:16**********************************@microsof t.com...
>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<Trecords).

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" <Ad*********@NOSPAMMYSPAM.omnibus.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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,
=========
"A.Rocha" <ar**********@ifthensoftware.comwrote in message
news:89**********************************@microso ft.com...
>>Hi,

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

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

anyone can help me?

--
Best regards,
A.Rocha

Nov 5 '08 #4

P: n/a
"A.Rocha" <ar**********@ifthensoftware.comwrote in message
news:89**********************************@microsof t.com...
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.
Nov 5 '08 #5

P: n/a
"Adam Benson" <Ad*********@NOSPAMMYSPAM.omnibus.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
List<won't serialize. You need to write some code to manually serialize
it.
Why wouldn't it serialize? List<Tis 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.

Nov 5 '08 #6

P: n/a
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.
--
Best regards,
A.Rocha
(Portugal)

"Jeff Johnson" <i.***@enough.spamescreveu na mensagem
news:%2***************@TK2MSFTNGP05.phx.gbl...
"A.Rocha" <ar**********@ifthensoftware.comwrote in message
news:89**********************************@microsof t.com...
>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.
Nov 5 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.