C# 序列化

XmlSerializer ser = new XmlSerializer(typeof(Report));
if (Directory.Exists("c:\\工作报告\\" + report.toBoss + "\\" + report.name))
{
      string filename = date.Text;
      string filepath = "c:\\工作报告\\" + report.toBoss + "\\" + report.name + "\\" + filename + ".xml";
        ser.Serialize(File.Create(filepath), report);
 }
  else
  {
        Directory.CreateDirectory("c:\\工作报告\\" + report.toBoss + "\\" + report.name);
        string filename = date.Text;
         string filepath = "c:\\工作报告\\" + report.toBoss + "\\" + report.name + "\\" + filename + ".xml";
        ser.Serialize(File.Create(filepath), report);
  }


XmlSerializer ser = new XmlSerializer(typeof(Report));
Report rep = (Report)ser.Deserialize(file.OpenText()) as Report;
[XmlRoot("REPORT")]
    public class Report
    {
      [XmlAttribute("NAME")]
        public string name { get;set;}
        [XmlElement("DATE")]
        public string date { get; set; }
        [XmlElement("CONTENT")]
        public string content { get; set; }
        [XmlElement("TOBOSS")]
        public string toBoss { get; set; }
    }
原文地址:https://www.cnblogs.com/liuxinls/p/2915064.html