正文

SelectNodes2007-11-02 15:14:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/30687.html

分享到:

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>

阅读(4177) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册