XmlSerializer用法

using System.IO;
using System.Xml.Serialization;

namespace DncSoft.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine(11);
            Save();
            System.Console.ReadLine();
            Read();
        }

        private static void Save()
        {
            JobLogType jobLog = new JobLogType();
            jobLog.name = "aaaaaaaaaaaa";
            jobLog.detailType = new JobLogDetailType();
            jobLog.detailType.startTime = 0;
            jobLog.detailType.endTime = 100;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(JobLogType));
            using (FileStream fs = new FileStream(@"c:\test.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fs, jobLog);
            }
        }

        private static void Read()
        {
            JobLogType jobLog = new JobLogType();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(JobLogType));
            using (FileStream fs = new FileStream(@"c:\test.xml", FileMode.Open))
            {
                jobLog = (JobLogType)xmlSerializer.Deserialize(fs);
            }
            System.Console.WriteLine(jobLog.name);
        }

    }

    public class JobLogType
    {
        public string name;
        public JobLogDetailType detailType;
    }

    public class JobLogDetailType
    {
        public int startTime;
        public int endTime;
    }

    public interface IJobLogService
    {
        JobLogType GetJogLog();
    }

}
 

你可能感兴趣的:(C#笔记)