为何XmlNode的节点值为空?
csd08 2005-08-26 06:03:51 我的代码:
public void ParseXMlFile(string filename)
{
// 创建一个XmlTextReader类的对象并调用Read方法来读取文件
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( filename);
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取Employees节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.Value!=null)
listBox1.Items.Add(" - NodeValue"+xe.Value.ToString());
listBox1.Items.Add("Name:"+xe.Name);
listBox1.Items.Add(" - AttrCount:"+xe.Attributes.Count.ToString());
listBox1.Items.Add(" - NodeType:"+xe.NodeType.ToString());
}
我每次xe.Value都是null,为什么啊?而且,HasChildNodes都是true的,明明没有了子节点,还是true!我都不知道是什么原因啦。
另外,如果我用XmlTextReader则是可以获取到节点值的,难道XmlDocument 就不可以吗?
各位fans,请多多帮忙解决啊。
我的xml文件是这样的:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
我是用C#写的。
^_^