日期:2014-05-20 浏览次数:20810 次
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中?? } }
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
{}
------解决方案--------------------