用C#怎么读取XML指定节点下的值
1个回答
展开全部
这是参考代码,差不多可以结贴采纳我吧。
public SqlConnection GetCon()
{
SqlConnection G_Con = null; ;
try
{
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "参数配置.xml");
if (File.Exists(configFile))
{
XmlOperator xmlOperator = new XmlOperator(configFile);
int size = xmlOperator.GetXmlReader().Length;
string[] getDataBaseInfo = new string[size];
for (int i = 0; i < size; i++)
{
getDataBaseInfo[i] = xmlOperator.GetXmlReader()[i];
}
string G_Str_ConnectionString = "Server=" + getDataBaseInfo[0];
G_Str_ConnectionString += ";user id=" + getDataBaseInfo[1];
G_Str_ConnectionString += ";password=" + getDataBaseInfo[2];
G_Str_ConnectionString += ";initial catalog=" + getDataBaseInfo[3] + ";Connect Timeout=20";
G_Con = new SqlConnection(G_Str_ConnectionString);
}
else
{
MessageBox.Show("不存在配置文件,请添加!");
Application.Exit();
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show("数据库打开失败,错误原因:" + ex.Message.ToString());
}
return G_Con;
}
public SqlConnection GetCon()
{
SqlConnection G_Con = null; ;
try
{
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "参数配置.xml");
if (File.Exists(configFile))
{
XmlOperator xmlOperator = new XmlOperator(configFile);
int size = xmlOperator.GetXmlReader().Length;
string[] getDataBaseInfo = new string[size];
for (int i = 0; i < size; i++)
{
getDataBaseInfo[i] = xmlOperator.GetXmlReader()[i];
}
string G_Str_ConnectionString = "Server=" + getDataBaseInfo[0];
G_Str_ConnectionString += ";user id=" + getDataBaseInfo[1];
G_Str_ConnectionString += ";password=" + getDataBaseInfo[2];
G_Str_ConnectionString += ";initial catalog=" + getDataBaseInfo[3] + ";Connect Timeout=20";
G_Con = new SqlConnection(G_Str_ConnectionString);
}
else
{
MessageBox.Show("不存在配置文件,请添加!");
Application.Exit();
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show("数据库打开失败,错误原因:" + ex.Message.ToString());
}
return G_Con;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询