Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute

第二种:XmlSerializer XmlAttribute

1.CustomConfiguration.xml

2.CustomConfigurationSetting.cs

3.CustomConfigurationManger.cs

4.TestXmlSerializer

1.CustomConfiguration.xml

<?xml version="1.0" encoding="utf-8" ?>
<CustomSettings>
    <SElement name="name01" value="value01" />
    <M2Element>
        <SElement name="name02" value="value02" />
        <SElement name="name03" value="value03" />
    </M2Element>
</CustomSettings>

2.CustomConfigurationSetting.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections;

namespace CustomConfigurationSection
{
    [XmlRoot("CustomSettings")]
    public class CustomConfigurationSettings
    {
        [XmlElement("SElement")]
        public SElementClazz SElement;

        private ArrayList _sElementList = new ArrayList();

        [XmlArrayItem("SElement")]
        [XmlArray("M2Element")]
        public SElementClazz[] SElementCollection
        {
            get
            {
                SElementClazz[] items = new SElementClazz[_sElementList.Count];
                _sElementList.CopyTo(items);
                return items;
            }
            set {
                SElementClazz[] items = (SElementClazz[])value;
                _sElementList.Clear();
                foreach (SElementClazz i in items)
                    _sElementList.Add(i);
            }
        }

    }

    public class SElementClazz
    {
        private string _name;
        private string _value;

        [XmlAttribute("name")]
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }

        [XmlAttribute("value")]
        public string Value
        {
            get { return this._value; }
            set { this._value = value; }
        }
    }
}

3.CustomConfigurationManager.cs

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;

namespace CustomConfigurationSection
{
    public class CustomConfigurationManager
    {
        public static T Create<T>(string config, string path)
        {
            if (!File.Exists(config))
            {
                return default(T);
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(config);
            XmlNode section = doc.SelectSingleNode(path);
            return Create<T>(null, null, section);
        }

        public static T Create<T>(object parent, object configContext, System.Xml.XmlNode section)
        {
            T settings = default(T);
            if (section == null) { return settings; }
            XmlSerializer xs = new XmlSerializer(typeof(T));
            XmlNodeReader reader = new XmlNodeReader(section);
            settings = (T)xs.Deserialize(reader);
            return settings;
        }
    }
}

4.TestXmlSerializer.cs

        public void TestXmlserializer()
        {
            try
            {
                CustomConfigurationSettings config = CustomConfigurationManager.Create<CustomConfigurationSettings>("CustomConfiguration.xml", "/CustomSettings");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

http://blog.csdn.net/zpx3243/article/details/5978604

原文地址:https://www.cnblogs.com/tianciliangen/p/7146927.html