如何读取xml指定节点下的值

孤剑 2006-02-18 05:30:30
data.xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<Configuration xmlns="http://tempuri.org/Configuration.xsd">
<Time>2006-2-18 14:18:06</Time>
<FileInfo>
<Application>
<ProductName>LetBetter</ProductName>
<UserAppDataRegistry>>HKEY_CURRENT_USER\Software\LetBetter\LetBetter\1.0.2240.25713 [0xd50]</UserAppDataRegistry>
<CompanyName>LetBetter</CompanyName>
<Language>System.Windows.Forms.InputLanguage</Language>
<CommonDataPath>C:\Documents and Settings\All Users\Application Data\LetBetter\LetBetter\1.0.2240.25713</CommonDataPath>
<Ver>1.0.2240.25713</Ver>
<Path>E:\VS.Net2003\LetBetter\bin\Debug</Path>
</Application>
<More>
<Status>C:\Documents and Settings\cbtegmy\Application Data\LetBetter\LetBetter\1.0.2240.25713</Status>
</More>
</FileInfo>
<server>
<ServerName>dsj</ServerName>
<DataBase>custom</DataBase>
<UserID>sa</UserID>
</server>
</Configuration>

如果我想读取server节点下的数据servername,database,userid的值,并将其值复制到string 类型中的Name,DataBase,UID中,我使用了下面的代码,但是不好时,请赐教:

我的代码:
/// <summary>
/// 读取先前的配置文件
/// </summary>
/// <returns></returns>
public bool ReadLoginData()
{

if(System.IO.File.Exists(Application.StartupPath+"\\data.xml")) //判断是否存在data.xml文件
{
try
{
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Application.StartupPath+"\\data.xml");
XmlNodeList nl=xmlDoc.SelectSingleNode("//Configuration/server").ChildNodes;
????
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
...全文
549 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤剑 2006-02-19
  • 打赏
  • 举报
回复
感谢 net_lover(孟子E章) 提供的代码,我已经测试成功了。谢谢!

孟子E章 2006-02-18
  • 打赏
  • 举报
回复
XmlNode x = xmlDoc.SelectSingleNode("//server");
if(x!=null)
{
string Name = (x.SelectSingleNode("ServerName")).InnerText;
string DataBase = (x.SelectSingleNode("DataBase")).InnerText;
...
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧