List of Class To XML File ,C#

if you create a collection class that have a list of Class Like :


    public class Foo
    {
        public List BarList { get; set; }
    }

    public class Bar
    {
 public string Property1 { get; set; }
 public string Property2 { get; set; }
    }
we can use XmlSerializer to Save Collection to XML File

Foo f = new Foo(); f.BarList = new List(); 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); }

Comments

Popular posts from this blog

C# Crop white space from around the image

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=xx.0.0.0, Culture=neutral, PublicKeyToken='xxx' or one of its dependencies.

The specified version string contains wildcards, which are not compatible with determinism.