Skip to main content
 首页 » 编程设计

c#之使用 C# 将 Config 部分绑定(bind)到 DataTable

2024年02月24日32lovecherry

我的 app.config 文件中有以下配置部分以及遍历配置部分以检索值的代码。但我想以适当的结构将配置部分的值保存到数据表中。如何 ?我想用适当的列显示 datagridview 中的所有值。

  <configSections> 
    <section name="ServerInfo" type="System.Configuration.IConfigurationSectionHandler" /> 
  </configSections> 
 
  <ServerInfo> 
    <Server id="1"> 
      <Name>SRUAV1</Name> 
      <key> 1 </key> 
      <IP>10.1.150.110</IP> 
      <Port>7901</Port>  
    </Server> 
    <Server id="2"> 
      <Name>SRUAV2</Name> 
      <key> 4 </key> 
      <IP>10.1.150.110</IP> 
      <Port>7902</Port> 
    </Server> 
    <Server id="3"> 
      <Name>SRUAV3</Name> 
      <key> 6 </key> 
      <IP>10.1.150.110</IP> 
      <Port>7904</Port> 
    </Server> 
  </ServerInfo> 

代码:

public void GetServerValues(string strSelectedServer) 
    { 
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
        ConfigurationSection section = config.GetSection("ServerInfo");  
        XmlDocument xml = new XmlDocument(); 
        xml.LoadXml(section.SectionInformation.GetRawXml()); 
        string temp = ""; 
        XmlNodeList applicationList = xml.DocumentElement.SelectNodes("Server");  
        for (int i = 0; i < applicationList.Count; i++) 
        { 
            object objAppId = applicationList[i].Attributes["id"]; 
            int iAppId = 0; 
            if (objAppId != null) 
            { 
                iAppId = Convert.ToInt32(applicationList[i].Attributes["id"].Value); 
            }  
            temp = BuildServerValues(applicationList[i]); 
        }  
    } 
 
    public string BuildServerValues(XmlNode applicationNode) 
    { 
        for (int i = 0; i < applicationNode.ChildNodes.Count; i++) 
        { 
            if (applicationNode.ChildNodes.Item(i).Name.ToString().Equals("Name")) 
            { 
                strServerName = applicationNode.ChildNodes.Item(i).InnerXml.ToString(); 
            } 
            if (applicationNode.ChildNodes.Item(i).Name.ToString().Equals("IP")) 
            { 
                strIP = applicationNode.ChildNodes.Item(i).InnerXml.ToString();  
            } 
            if (applicationNode.ChildNodes.Item(i).Name.ToString().Equals("Port")) 
            { 
                strPort = applicationNode.ChildNodes.Item(i).InnerXml.ToString();  
            } 
        } 
        return strServerName; 
    } 

请您参考如下方法:

我更喜欢使用现有的配置类来构建强类型配置部分。首先,我将从服务器元素本身开始:

public class ServerConfigurationElement : ConfigurationElement 
{ 
  [ConfigurationProperty("id")] 
  public int Id { 
    get { return (int)this["id"]; } 
    set { this["id"] = value; } 
  } 
 
  [ConfigurationProperty("name")] 
  public string Name {  
    get { return (string)this["name"]; } 
    set { this["name"] = value; } 
  } 
 
  [ConfigurationProperty("key")] 
  public int Key { 
    get { return (int)this["key"]; } 
    set { this["key"] = value; 
  } 
 
  [ConfigurationProperty("ip")] 
  public string IPAddress { 
    get { return (string)this["ip"]; } 
    set { this["ip"] = value; } 
  } 
 
  [ConfigurationProperty("port")] 
  public int Port { 
    get { return (int)this["port"]; } 
    set { this["port"] = value; } 
  } 
} 

这为服务器的单一配置指定了一个模型。然后我们需要创建一个集合类

[ConfigurationCollection(typeof(ServerConfigurationElement), AddItemName = "server")] 
public class ServerConfigurationElementCollection : ConfigurationElementCollection 
{ 
  protected override CreateNewElement() { 
    return new ServerConfigurationElement(); 
  } 
 
  protected override object GetElementKey(ConfigurationElement element) { 
    return ((ServerConfigurationElement)element).Id; 
  } 
} 

这允许配置系统创建服务器配置项的集合。最后,我们创建一个部分:

public class ServerConfigurationSection : ConfigurationSection 
{ 
  [ConfigurationProperty("servers")] 
  public ServerConfigurationElementCollection Servers { 
    get { return (ServerConfigurationElementCollection)this["servers"]; } 
    set { this["servers"] = value; } 
  } 
 
  public static ServerConfigurationSection GetConfiguration() { 
    return ConfigurationManager.GetSection("ServerInfo") as ServerConfigurationSection; 
  } 
} 

标记与您的略有不同:

<configSections> 
  <section name="ServerInfo" type="ServerConfigurationSection, MyAssembly" /> 
</configSections> 
 
<ServerInfo> 
  <servers> 
    <server id="1" name="SRUAV1" ip="10.1.150.110" port="7901" /> 
  </servers> 
</ServerInfo> 

然后我们可以在代码中使用它:

/// <summary> 
/// Binds the server configurations to the specified grid view. 
/// </summary> 
public void BindConfiguration(DataGridView gridView) { 
  if (gridView == null) 
    throw new ArgumentNullException("gridView"); 
 
  var config = ServerConfigurationSection.GetConfiguration(); 
  if (config != null) { 
    gridView.DataSource = config.Servers.Cast<ServerConfigurationElement>().ToList(); 
  } 
} 

我所做的是提取当前配置,并通过列表将元素绑定(bind)到数据网格。

希望对您有所帮助。