C# 将 xml 配置文件转换成自定义对象

98 阅读1分钟

需求:列表的列支持动态可配置。

过程:从 xml 文件中读取列的配置项,构建列表。

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<customerColumn>
		<customerId>true</customerId>
		<customerName>true</customerName>
		<contactName>true</contactName>
	</customerColumn>
</Configuration>

customerColumn是列表的列对象,customerIdcustomerNamecontactName是列信息。只有当列的值是 true 时才会显示。

CustomerColumn 类

public class CustomerColumn
{
        public string customerId { get; set; }

        public string customerName { get; set; }

        public string contactName { get; set; }
}

CustomerColumn中的属性 customerIdcustomerNamecontactName必须要保持和 customerColumn中的嵌套子项的命名保持一致。

从 xml 到 object

public void LoadConfiguration()
{
        try
        {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml";
                Configuration configuration = ConfigHelper.GetConfigInfo<Configuration>(filePath);
                SetupCustomerTable(configuration.customerColumn);
        }
        catch(Exception ex)
        {
                configuration = null;
                LogHelper.Error(ex.Message);
        }
}

Configuration configuration = ConfigHelper.GetConfigInfo<Configuration>(filePath);完成了从配置项到对象属性的转化。这里的核心是 XmlSerializer对象的使用。

class ConfigHelper
{
        public static T GetConfigInfo<T>(string xmlPath) where T : class
        {
                // // T t=default(T),就是初始化,值类型的话,就是T t=0;引用类型的话,就是T t=null
                T configInfo = default(T); 
                try
                {
                        object obj = Deserialize(typeof(T), xmlPath);
                        if (obj != null && obj is T)
                        {
                                configInfo = (T)obj;
                        }
                }
                catch (Exception)
                {
                        LogHelper.Log("GetConfigInfo获取配置信息失败");
                }
                return configInfo;
        }

        public static object Deserialize(Type type, string xmlPath)
        {
                object result = null;
                try
                {
                        using (StreamReader sr = new StreamReader(xmlPath))
                        {
                                XmlSerializer xmldes = new XmlSerializer(type);
                                result = xmldes.Deserialize(sr);
                        }
                }
                catch (Exception)
                {
                        LogHelper.Log("Deserialize反序列化失败");
                }
                return result;
        }
}
public void SetupCustomerTable()
{
        // todo 反射技术
        DataTable dt = new DataTable();
        if (configuration.customerColumn.customerId == "true")
        {
                dt.Columns.Add("CustomerId", Type.GetType("System.String"));
        }
        if (configuration.customerColumn.customerName == "true")
        {
                dt.Columns.Add("CustomerName", Type.GetType("System.String"));
        }
        if (configuration.customerColumn.contactName == "true")
        {
                dt.Columns.Add("ContactName", Type.GetType("System.String"));
        }
}