本来是把连接字符串写在代码中:
public static string connectionString = "Server=192.192.192.1;Database=TEST;Uid=TEST;Pwd=TEST;"现改到配置文件sys.xml中:
<DataPath>
<DataPath name="ConnectionString" value="Server=192.192.192.1;Database=TEST;Uid=TEST;Pwd=TEST;">
</DataPath>
</DataPath>那代码中如何写才能得到?
public static string connectionString = "Server=192.192.192.1;Database=TEST;Uid=TEST;Pwd=TEST;"现改到配置文件sys.xml中:
<DataPath>
<DataPath name="ConnectionString" value="Server=192.192.192.1;Database=TEST;Uid=TEST;Pwd=TEST;">
</DataPath>
</DataPath>那代码中如何写才能得到?
读取方法很简单!
ConfigurationSettings.AppSettings[configName]
foreach (XmlNode xn in nodeList)
{
string n = xn.Name;
string k = xn.Attributes["name"].Value;
string v = xn.Attributes["value"].Value;
string D = xn.Attributes["Database"].Value;
string U = xn.Attributes["Uid"].Value;
string P = xn.Attributes["Pwd"].Value;
listBox1.Items.Add(n+"|"+k + ":" + v);
}
using System.IO;
其实还不如写到appconfig中如果 是web的话写的webconfig中
比较方便
正解,可以建立一个专门读取xml的类,使用时直接拿来用就可以了!!
myds.ReadXml(@"D:\Sys.xml");
DataTable mytable=new DataTable();
string something = myds.Tables[0].Rows[0]["value"].ToString().Trim();
textBox1.Text=something;
string something = myds.Tables[0].Rows[1]["value"].ToString().Trim();
关键字: c#读取xml文件的节点内容
下面的xml文件包含不同的节点 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<user id="1">
<name>admin</name>
<password>123456</sex>
</user>
<user id="2">
<name>guest</name>
<password>654321</password>
</user>
<upload>
<saveToPath>/temp</saveToPath>
<maxSize>10240</maxSize>
<forbiddenFileExts>.exe|.jsp|.asp|.aspx</forbiddenFileExts>
</upload>
<InspectionInterval>3</InspectionInterval>
</configuration> 这种有重复节点的XML最好用表结构来读取
方法:
DataSet ds = new DataSet();
ds.ReadXml(@"E:\XMLFile1.xml");//xml文件完整路径
对于你的这段xml文件,ds会读成3个表,configuration表(1行),user表(2行),upload表(1行);
例如要取 user id = 2时的name值,相当于取user表的第二行,name列的值,可以这样:
string name = ds.Tables["user"].Rows[1]["name"].ToString();
取saveToPath值:
ds.Table["upload"].Rows[0]["saveToPath"];