Saving a POCO to XML

  • Add using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;

namespace Northwind.Training.PocosAndXml
{
    class Demo
    { 
... 
  • Create the SaveXml Method
public void SaveXml(OrderPoco o, string fileName)
{
    using (var sw = new StreamWriter(fileName))
    {
        new XmlSerializer(typeof(OrderPoco)).Serialize(sw, o);
    }
} 
  • Using the SaveXml Method
public void Run()
{
    var o = new OrderPoco
    {
        OrderID = 1,
        CustomerID = "ABCDE",
        Shipper = 5
    };
    SaveXml(o, @"c:\temp\orders.xml");
} 

Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com