public class MyData { public int a; public string b; public MyData() { } public MyData(int _a, string _b) { a = _a; b = _b; } }
class Program { static void Main(string[] args) { RunIt(); } static void RunIt() { MyData v1 = new MyData(0, "Zero"); MyData v2 = new MyData(1, "One"); MyData v3 = new MyData(2, "Two"); ArrayList al = new ArrayList(); al.Add(v1); al.Add(v2); al.Add(v3); XmlSerializer s = new XmlSerializer(typeof(ArrayList)); string path = @"c:\seri.xml"; TextWriter tx = new StreamWriter(path, true); s.Serialize(tx, al); tx.Close(); FileStream fs = File.OpenRead(@"c:\seri.xml"); ArrayList ax = (ArrayList)s.Deserialize(fs); Console.WriteLine(((MyData)ax[0]).a.ToString()); Console.WriteLine(((MyData)ax[1]).a.ToString()); Console.WriteLine(((MyData)ax[2]).a.ToString()); } }
XmlSerializer s = new XmlSerializer(typeof(ArrayList),new Type[]{typeof(MyData)});