• 将对象XML序列化为XML文件/反序列化XML文件为对象


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using NPOI.HSSF.UserModel;
    using NPOI.XSSF;
    using NPOI.XSSF.UserModel;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //XML序列化对象集合
                //Department dep = new Department();
                //dep.Name = "事业部";
                //dep.Teams = new List<Team>();
                //dep.Teams.Add(new Team("Test", "测试团队"));
                //dep.Teams.Add(new Team("Develop", "开发团队"));
    
                //dep["Test"].Persons = new List<Person>();
                //dep["Test"].Persons.Add(new Person("dwf", 22, "程序", "测试工程师"));
                //dep["Test"].Persons.Add(new Person("WL", 22, "代码", "测试工程师"));
    
                //dep["Develop"].Persons = new List<Person>();
                //dep["Develop"].Persons.Add(new Person("dwf", 22, "程序", "开发工程师"));
                //dep["Develop"].Persons.Add(new Person("WL", 22, "代码", "高级开发工程师"));
    
                //XmlSerializer serializer = new XmlSerializer(dep.GetType());
                //TextWriter writer = new StreamWriter("Department.xml");
    
                //serializer.Serialize(writer, dep);
                //writer.Close();
    
                //Machine = "WB01", Type = "BLT&Tilting", Project = "S010", Sample = 5 
                //FileFormat file = new FileFormat();
                //file.Name = "IT";
    
                //file.Modules = new List<Module>();
    
                //file.Modules.Add(new Module("WB01", "BLT&Tilting", "S010", 5,"Format1"));
                //file.Modules.Add(new Module("WB02", "BTT&Tilting", "T4061", 5, "Format1"));
    
                //XmlSerializer serializer = new XmlSerializer(file.GetType());
                //TextWriter writer = new StreamWriter("Formats.xml");
    
                //serializer.Serialize(writer, file);
                //writer.Close();
    
                //反序列化XML数据为对象集合
                //XmlSerializer serializer = new XmlSerializer(typeof(Department));
                //FileStream stream = new FileStream("Department.xml", FileMode.Open);
                //Department dep = (Department)serializer.Deserialize(stream);
                //stream.Close();
    
                XmlSerializer serializer = new XmlSerializer(typeof(FileFormat));
                FileStream stream = new FileStream("Formats.xml", FileMode.Open);
                FileFormat obj = (FileFormat)serializer.Deserialize(stream);
                stream.Close();
            }
        }
    
        public class FileFormat
        {
            [XmlAttribute]
            public string Name { get; set; }
    
            public List<Module> Modules;
    
            public Module this[string Name]
            {
                get
                {
                    Module m = null;
    
                    foreach (Module te in Modules)
                    {
                        if (string.Compare(te.Name, Name) == 0)
                        {
                            m = te;
                            break;
                        }
                    }
                    return m;
                }
            }
        }
    
        public class Module
        {
            public string Name;
            public string Machine;
            public string Type;
            public string Project;
            public int Sample;
    
            public Module()
            { }
    
            public Module(string machine,string type,string project ,int sample, string name)
            {
                this.Machine = machine;
                this.Type = type;
                this.Project = project;
                this.Sample = sample;
                this.Name = name;
            }
        }
    
        public class Department
        {
            public Department()
            {
            }
            [XmlAttribute]
            public string Name;
            public List<Team> Teams;
            public Team this[string Name]
            {
                get
                {
                    Team t = null;
                    foreach (Team te in Teams)
                    {
                        if (string.Compare(te.Name, Name) == 0)
                        {
                            t = te;
                            break;
                        }
                    }
                    return t;
                }
            }
        }
    
        public class Team
        {
            public Team()
            {
    
            }
            public Team(string Name, string Title)
            {
                this.Name = Name;
                this.Title = Title;
            }
            public string Name;
            public string Title;
            public List<Person> Persons;
    
        }
    
        public class Person
        {
            public Person()
            {
    
            }
            public Person(string Name, int Age, string Hobby, string Station)
            {
                this.Name = Name;
                this.Age = Age;
                this.Hobby = Hobby;
                this.Station = Station;
            }
            public string Name;
            public int Age;
            public string Hobby;
            public string Station;
        }
    }
  • 相关阅读:
    CodeBlocks下载与安装教程
    Delphi 资源管理器套件
    做了一个 62 进制的简单实现
    关于 TRegEx.Split()
    Delphi 的链式代码
    在Linux上编译dotnet cli的源代码生成.NET Core SDK的安装包
    尝试解决在构造函数中同步调用Dns.GetHostAddressesAsync()引起的线程死锁
    .NET Core中遇到奇怪的线程死锁问题:内存与线程数不停地增长
    将asp.net core站点发布到IIS上遇到的问题
    .NET Core 构建配置文件从 project.json 到 .csproj
  • 原文地址:https://www.cnblogs.com/YuanDong1314/p/13130484.html
Copyright © 2020-2023  润新知