专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > .NET Framework

怎么转换为二进制流

发布时间:2011-06-23 14:41:56 文章来源:www.iduyao.cn 采编人员:星星草
如何转换为二进制流?
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)
这样对吧??

------解决方案--------------------
这个得好好的研究
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: