2010-06-08 4 views
1

J'ai la section config suivante dans mon fichier app.config et le code pour parcourir la section de configuration pour récupérer les valeurs. Mais je veux sauvegarder les valeurs de la section config dans une datatable dans une structure appropriée. Comment ? Je veux montrer toutes les valeurs dans datagridview avec les colonnes appropriées.Lier la section Config à DataTable en utilisant C#

<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> 

code:

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

Répondre

3

Je préfère utiliser les classes de configuration existantes pour construire une section de configuration fortement typé. Tout d'abord, je commencerai par l'élément serveur lui-même:

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

Ceci spécifie un modèle pour une configuration singulière d'un serveur. Nous devons ensuite créer une classe de collection

[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; 
    } 
} 

Cela permet au système de configuration de créer une collection d'éléments de configuration du serveur. Enfin, nous créons une section:

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

Le balisage est légèrement différente de la vôtre:

<configSections> 
    <section name="ServerInfo" type="ServerConfigurationSection, MyAssembly" /> 
</configSections> 

<ServerInfo> 
    <servers> 
    <server id="1" name="SRUAV1" ip="10.1.150.110" port="7901" /> 
    </servers> 
</ServerInfo> 

On peut alors l'utiliser dans le code:

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

Ce que je l'ai fait est J'ai sorti la configuration actuelle, et lié les éléments à la grille de données via une liste.

Espérons que ça aide.

Questions connexes