Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c将配置节绑定到DataTable_C# - Fatal编程技术网

C# 使用c将配置节绑定到DataTable

C# 使用c将配置节绑定到DataTable,c#,C#,我的app.config文件中有下面的config部分,以及遍历config部分以检索值的代码。但是我想将config节的值保存到一个适当结构的数据表中。怎样我想用适当的列显示datagridview中的所有值 <configSections> <section name="ServerInfo" type="System.Configuration.IConfigurationSectionHandler" /> </configSections&

我的app.config文件中有下面的config部分,以及遍历config部分以检索值的代码。但是我想将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();
  }
}
我所做的是拉出当前配置,并通过列表将元素绑定到datagrid


希望能有所帮助。

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

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();
  }
}
我所做的是拉出当前配置,并通过列表将元素绑定到datagrid

希望有帮助