473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Write to file a List<type>

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
6 14524
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
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
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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rainer Queck | last post by:
Hi NG, I have a base class "Telegram". This class , as well as all descants have a static public field "public static int TelegramNo = <a tlg no>;" Now I added all descants of Telegram to a...
18
by: Sean | last post by:
I have been using List(of String) when I could easily be using a string array instead. Is it still considered best practice to use Generic list of string rather then a string array? Thanks
3
by: Web learner | last post by:
The following code works fine private List<double> GetDataFor(string column, int selectedYear) { ------- ------- return list; } foreach (double item in GetDataFor("AirTemp", selectedYear))...
4
by: Rene | last post by:
According to the documentation, the List<T> type explicitly implements the non generic IList interface. The problem is that no matter how hard I look, I am not able to find this implemetion on...
13
by: Murat Ozgur | last post by:
Hello, Is there any difference between ArrayList and List<object? Which one should I use ? Thanks.
7
by: daokfella | last post by:
I have a business object that exposes a collection of other objects via a List<of Type>. How can I intercept when an item is either added or removed from this list. Is it possible? private...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a list of type object. The object has an ID as one of the elements and I would like to create another list that just has objects with unique IDs. For example in the list if I have...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.