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; }
    }
    public class Foo
    {
        public List<bar> BarList { get; set; }
    }

    public class Bar
    {
 public string Property1 { get; set; }
 public string Property2 { get; set; }
    }
</bar>
  1. <code>
  2. public class Foo
  3. {
  4. public List<bar> BarList { get; set; }
  5. }
  6. public class Bar
  7. {
  8. public string Property1 { get; set; }
  9. public string Property2 { get; set; }
  10. }
  11. </bar></code>
<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>
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); }
<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>
  1. <code>
  2. <div style="overflow-x: scroll;">
  3. <div style="width: 1324px;">
  4. Foo f = new Foo();
  5. f.BarList = new List<bar>();
  6. f.BarList.Add(new Bar { Property1 = "abc", Property2 = "def" });
  7. XmlSerializer ser = new XmlSerializer(typeof(Foo));
  8. using (FileStream fs = new FileStream(@"c:\sertest.xml", FileMode.Create))
  9. {
  10. ser.Serialize(fs, f);
  11. }
  12. </bar></div>
  13. </div>
  14. </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>

Popular posts from this blog

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

C# Crop white space from around the image

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