取不到XML文档的值,急死了,大侠们请进
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
XmlDocument xmdoc = new XmlDocument();
xmdoc.Load("H:\\ConsoleApplication1\\ConsoleApplication1\\test.xml");
int num = xmdoc.DocumentElement.ChildNodes.Count;
Console.WriteLine(num);
XmlNode nod = xmdoc.SelectSingleNode("employee/sex");
string notevalue = nod.Value;
if (notevalue == null)
{
Console.WriteLine("no value");
}
else
{
Console.WriteLine(notevalue);
}
}
catch (FileNotFoundException e)
{
string exp = e.FileName;
Console.WriteLine( exp);
}
//finally
//{
// Console.WriteLine("successful");
//}
}
}
}
为什么nod总是取不到值啊,xmdoc.SelectSingleNode("employee/sex")这个语句有错吗?
以下是XML文件
<?xml version="1.0" encoding="utf-8" ?>
<employee>
<name>xiaohong</name>
<sex>female</sex>
<dept>IT</dept>
</employee>
------解决方案--------------------把
string notevalue = nod.Value;
换成以下一行就可以了
string notevalue = nod.InnerText;