当前位置: 代码迷 >> .NET Framework >> 怎么转换为二进制流
  详细解决方案

怎么转换为二进制流

热度:102   发布时间:2016-05-02 00:57:00.0
如何转换为二进制流?
C# code
    class Student    {        private int id;        private string name;        public string Name        {            get { return name; }            set { name = value; }        }        public int Id        {            get { return id; }            set { id = value; }        }    }    class Program    {        static void Main(string[] args)        {            Student student1 = new Student();            Student student2 = new Student();            Student student3 = new Student();            student1.Id = 1;            student1.Name = "a";            student2.Id = 2;            student2.Name = "b";            student3.Id = 3;            student3.Name = "c";            ArrayList arraylist = new ArrayList();            arraylist.Add(student1);            arraylist.Add(student2);            arraylist.Add(student3);            //我怎么把arraylist中的对象放入txt中??        }    }

//我怎么把arraylist中的对象放入txt中??

最好能给我个例子 谢谢!

------解决方案--------------------
序列化后存起来就可以了。
------解决方案--------------------
C# code
try            {                FileStream fs = new FileStream("feed.txt", FileMode.Create);                //2.构建二进制格式化对象                BinaryFormatter bf = new BinaryFormatter();                //3.调用序列化方法                bf.Serialize(fs, student1 );//把student1 对象保存到"feed.txt                fs.Close();            }            catch(Exception ex)            {                throw ex;            }
------解决方案--------------------
XmlSerializer xs = new XmlSerializer(typeof(Student));
MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.Default);

 [Serializable]
public class Student
{}
------解决方案--------------------
探讨

引用:
类名前加
[Serializable]


我的对象是arraylist 加入也没关系的吧?


[Serializable]
class Program
{
static void Main(string[] args)
这样对吧??

------解决方案--------------------
这个得好好的研究
  相关解决方案