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