c# Form程序 获取XML的属性值

来源:百度知道 编辑:UC知道 时间:2024/09/23 20:14:32
比如下面的XML:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ExportConnectionString" value="ODBC;Driver=SQL Server;Server=10.80.64.35;Uid=scuser;Pwd=scuser;database=sc_data"></add>
<add key="DBConnectionString" value="Provider=SQLOLEDB.1;Initial Catalog=sc_data;Data Source=10.142.73.14;User ID=scuser;password=wzztlfwjysl"></add>
</appSettings>
</configuration>

我想获取第2个add的value属性值
不要把它看成配置文件,把它看成XML文件
用xmldocument xdom=new xmldocument ();
xmlnode xnl=xdom.SelectSingleNode("这里的表达式怎么写??????")
然后怎么得到add 的节点

XmlDocument xmlDoc = new XmlDocument ( );
xmlDoc.Load ( "App1.config" );

XmlNodeList list = xmlDoc.GetElementsByTagName ( "add" );
string[] keys = new string [ list.Count ];
for(int i = 0; i < list.Count; i++)
{
if ( list[i].Name == "add" )
{
keys [ i ] = list [ i ].Attributes [ "value" ].Value;
}
}

MessageBox.Show ( keys[1] );

注意app1.config的编译选项应该选择"复制"

System.Configuration.ConfigurationManager.AppSettings["DBConnectionString"]

其中,AppSettings[] 里面填写你所要引用的 value 前的 key 那个值 就可以了。

以上是 C#.NET 2.0 写法, 如果是 2.0以下版本就用:

1.0时是:
System.Configuration.ConfiguratonSetings.AppSetings
2.0
是System.Configuration.ConfigurationManager.AppSettings