List of Class To XML File ,C#
if you create a collection class that have a list of Class Like :
we can use XmlSerializer to Save Collection to XML File
- <code>
- public class Foo
- {
- public List<bar> BarList { get; set; }
- }
- public class Bar
- {
- public string Property1 { get; set; }
- public string Property2 { get; set; }
- }
- </bar></code>
- <code>
- <div style="overflow-x: scroll;">
- <div style="width: 1324px;">
- Foo f = new Foo();
- f.BarList = new List<bar>();
- f.BarList.Add(new Bar { Property1 = "abc", Property2 = "def" });
- XmlSerializer ser = new XmlSerializer(typeof(Foo));
- using (FileStream fs = new FileStream(@"c:\sertest.xml", FileMode.Create))
- {
- ser.Serialize(fs, f);
- }
- </bar></div>
- </div>
- </code>