How to Load Config File Programmatically

前端 未结 4 1625
[愿得一人]
[愿得一人] 2020-12-01 06:42

Suppose I have a Custom Config File which corresponds to a Custom-defined ConfigurationSection and Config elements. These config classes are stored in a library.

C

4条回答
  •  感动是毒
    2020-12-01 07:25

    Following code will allow you to load content from XML into objects. Depending on source, app.config or another file, use appropriate loader.

    using System.Collections.Generic;
    using System.Xml.Serialization;
    using System.Configuration;
    using System.IO;
    using System.Xml;
    
    class Program
    {
        static void Main(string[] args)
        {
            var section = SectionSchool.Load();
            var file = FileSchool.Load("School.xml");
        }
    }
    

    File loader:

    public class FileSchool
    {
        public static School Load(string path)
        {
            var encoding = System.Text.Encoding.UTF8;
            var serializer = new XmlSerializer(typeof(School));
    
            using (var stream = new StreamReader(path, encoding, false))
            {
                using (var reader = new XmlTextReader(stream))
                {
                    return serializer.Deserialize(reader) as School;
                }
            }
        }
    }
    

    Section loader:

    public class SectionSchool : ConfigurationSection
    {
        public School Content { get; set; }
    
        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            var serializer = new XmlSerializer(typeof(School)); // works in     4.0
            // var serializer = new XmlSerializer(type, null, null, null, null); // works in 4.5.1
            Content = (Schoool)serializer.Deserialize(reader);
        }
        public static School Load()
        {
            // refresh section to make sure that it will load
            ConfigurationManager.RefreshSection("School");
            // will work only first time if not refreshed
            var section = ConfigurationManager.GetSection("School") as SectionSchool;
    
            if (section == null)
                return null;
    
            return section.Content;
        }
    }
    

    Data definition:

    [XmlRoot("School")]
    public class School
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
    
        [XmlElement("Student")]
        public List Students { get; set; }
    }
    
    [XmlRoot("Student")]
    public class Student
    {
        [XmlAttribute("Index")]
        public int Index { get; set; }
    }
    

    Content of 'app.config'

    
    
    
      
        

    Content of XML file:

    
    
      
      
      
      
      
      
      
      
      
    
    

    posted code has been checked in Visual Studio 2010 (.Net 4.0). It will work in .Net 4.5.1 if you change construction of seriliazer from

    new XmlSerializer(typeof(School))
    

    to

    new XmlSerializer(typeof(School), null, null, null, null);
    

    If provided sample is started outside debugger then it will work with simplest constructor, however if started from VS2013 IDE with debugging, then change in constructor will be needed or else FileNotFoundException will occurr (at least that was in my case).

提交回复
热议问题