Write to file a List<type>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A.Rocha

    #1

    Write to file a List<type>

    Hi,

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

    //
    List<mytypemyLi st = new List<mytype>();

    anyone can help me?

    --
    Best regards,
    A.Rocha
  • Adam Benson

    #2
    Re: Write to file a List&lt;type&gt ;

    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(S erializationInf o info,
    StreamingContex t context)
    {
    int i = 0;
    info.AddValue(" num", this.Count);
    foreach (T item in this)
    {
    info.AddValue(" item" + i.ToString(), item);
    ++i;
    }
    }

    protected MyList(Serializ ationInfo info, StreamingContex t context)
    {
    int num = info.GetInt32(" num");
    for (int i = 0; i < num; ++i)
    {
    T item = (T)info.GetValu e("item" + i.ToString(), typeof(T));
    base.Add(item);
    }
    }
    #endregion Manual serialization
    }

    HTH,

    Adam,
    =========


    "A.Rocha" <armandorocha@i fthensoftware.c omwrote in message
    news:899AFB3F-CF4B-467C-8CE7-3CBEA32A89A7@mi crosoft.com...
    Hi,
    >
    I need save to text file a List<type>, but i dont wont serialize.
    >
    //
    List<mytypemyLi st = new List<mytype>();
    >
    anyone can help me?
    >
    --
    Best regards,
    A.Rocha

    Comment

    • Stanimir Stoyanov

      #3
      Re: Write to file a List&lt;type&gt ;

      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" <Adam.Benson@NO SPAMMYSPAM.omni bus.co.ukwrote in message
      news:%2373sC8zP JHA.4008@TK2MSF TNGP02.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(S erializationInf o info,
      StreamingContex t context)
      {
      int i = 0;
      info.AddValue(" num", this.Count);
      foreach (T item in this)
      {
      info.AddValue(" item" + i.ToString(), item);
      ++i;
      }
      }
      >
      protected MyList(Serializ ationInfo info, StreamingContex t context)
      {
      int num = info.GetInt32(" num");
      for (int i = 0; i < num; ++i)
      {
      T item = (T)info.GetValu e("item" + i.ToString(),
      typeof(T));
      base.Add(item);
      }
      }
      #endregion Manual serialization
      }
      >
      HTH,
      >
      Adam,
      =========
      >
      >
      "A.Rocha" <armandorocha@i fthensoftware.c omwrote in message
      news:899AFB3F-CF4B-467C-8CE7-3CBEA32A89A7@mi crosoft.com...
      >Hi,
      >>
      >I need save to text file a List<type>, but i dont wont serialize.
      >>
      >//
      >List<mytypemyL ist = new List<mytype>();
      >>
      >anyone can help me?
      >>
      >--
      >Best regards,
      >A.Rocha
      >
      >

      Comment

      • A.Rocha

        #4
        Re: Write to file a List&lt;type&gt ;

        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" <stoyanov@REMOV ETHIS.live.come screveu na mensagem
        news:16C1B42B-B938-4DCC-8C38-834B86E11391@mi crosoft.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" <Adam.Benson@NO SPAMMYSPAM.omni bus.co.ukwrote in message
        news:%2373sC8zP JHA.4008@TK2MSF TNGP02.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(S erializationInf o info,
        >StreamingConte xt context)
        > {
        > int i = 0;
        > info.AddValue(" num", this.Count);
        > foreach (T item in this)
        > {
        > info.AddValue(" item" + i.ToString(), item);
        > ++i;
        > }
        > }
        >>
        > protected MyList(Serializ ationInfo info, StreamingContex t context)
        > {
        > int num = info.GetInt32(" num");
        > for (int i = 0; i < num; ++i)
        > {
        > T item = (T)info.GetValu e("item" + i.ToString(),
        >typeof(T));
        > base.Add(item);
        > }
        > }
        > #endregion Manual serialization
        >}
        >>
        >HTH,
        >>
        >Adam,
        >=========
        >>
        >>
        >"A.Rocha" <armandorocha@i fthensoftware.c omwrote in message
        >news:899AFB3 F-CF4B-467C-8CE7-3CBEA32A89A7@mi crosoft.com...
        >>Hi,
        >>>
        >>I need save to text file a List<type>, but i dont wont serialize.
        >>>
        >>//
        >>List<mytypemy List = new List<mytype>();
        >>>
        >>anyone can help me?
        >>>
        >>--
        >>Best regards,
        >>A.Rocha
        >>
        >>
        >

        Comment

        • Jeff Johnson

          #5
          Re: Write to file a List&lt;type&gt ;

          "A.Rocha" <armandorocha@i fthensoftware.c omwrote in message
          news:899AFB3F-CF4B-467C-8CE7-3CBEA32A89A7@mi crosoft.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.


          Comment

          • Alberto Poblacion

            #6
            Re: Write to file a List&lt;type&gt ;

            "Adam Benson" <Adam.Benson@NO SPAMMYSPAM.omni bus.co.ukwrote in message
            news:%2373sC8zP JHA.4008@TK2MSF TNGP02.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.

            Comment

            • A.Rocha

              #7
              Re: Write to file a List&lt;type&gt ;

              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.get@enough.s pamescreveu na mensagem
              news:%23k0QJb1P JHA.728@TK2MSFT NGP05.phx.gbl.. .
              "A.Rocha" <armandorocha@i fthensoftware.c omwrote in message
              news:899AFB3F-CF4B-467C-8CE7-3CBEA32A89A7@mi crosoft.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.
              >

              Comment

              Working...