using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
namespace SelectNodes
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"E:\c#\c#_windows程序设计学生成绩信息.xml");
XmlNodeList nodelist;
XmlElement root = doc.DocumentElement;
nodelist = root.SelectNodes("student/name");
foreach (XmlNode name1 in nodelist)
{
Console.WriteLine(name1.InnerText);
}
// 打印每个元素的innertext
IEnumerator nodepointer = root.GetEnumerator();
XmlNode student;
while (nodepointer.MoveNext())
{
student = (XmlElement)nodepointer.Current;
if (student.HasChildNodes)
{
nodelist = student.SelectNodes("*");
foreach (XmlNode temp in nodelist)
{
Console.Write(temp.InnerText + " ");
}
Console.WriteLine();
}
}
}
}
}
//学生成绩信息.xml 如下:
<?xml version="1.0" encoding="GBK"?>
<studentlist>
<student>
<name>A</name>
<num>8000103124</num>
<age>21</age>
<sex>male</sex>
<englishscores>91</englishscores>
<xmlscores>80</xmlscores>
<umlscores>85</umlscores>
<scholarship>特等</scholarship>
</student>
<student>
<name>B</name>
<num>8000104031</num>
<age>21</age>
<sex>male</sex>
<englishscores>90</englishscores>
<xmlscores>76</xmlscores>
<umlscores>95</umlscores>
<scholarship>一等</scholarship>
</student>
</studentlist>
评论