使用c#将Config部分绑定到DataTable

时间:2010-06-08 08:33:06

标签: c#

我的app.config文件中有以下配置部分,以及迭代config部分以检索值的代码。但我想将config部分的值保存到适当结构的数据表中。怎么样 ?我想用datagridview显示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;
    }

1 个答案:

答案 0 :(得分:3)

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

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();
  }
}

我所做的是我已经取出当前配置,并通过List将元素绑定到datagrid。

希望有所帮助。