793291696readxmltotext

readxmltotext

c#控制台应用程序
program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 读写
{
    internal class Program
    {
        static void Main(string[] args)
        {
             string xml_FilePath = "D:/DDD/scut.xml";
            XmlDocument xmlDocument = new XmlDocument();//新建一个XML“编辑器”
            xmlDocument.Load(xml_FilePath);//载入路径这个xml
            List<student> list = new List<student>();

            XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("grades").ChildNodes;//选择class为根结点并得到旗下所有子节点

            foreach (XmlNode xmlNode in xmlNodeList)//遍历class的所有节点
                {
                    XmlElement xmlElement = (XmlElement)xmlNode;//对于任何一个元素,其实就是每一个<student>

                    //旗下的子节点<name>和<number>分别放入dataGridView1
                    string t1 = xmlElement.ChildNodes.Item(0).InnerText;
                    int i=int.Parse(t1);
                    string t2= xmlElement.ChildNodes.Item(1).InnerText;
                    string t3= xmlElement.ChildNodes.Item(2).InnerText;
                    string t4 = xmlElement.ChildNodes.Item(3).InnerText;
                    int i4 = int.Parse(t4);
                    student t = new student(i,t2,t3,i4);
                    list.Add(t);
               } 
            
            list.Sort();
            string path = "D:/DDD/info.txt";
            FileInfo fi = new FileInfo(path);
            if (!fi.Exists) fi.Create().Close();
            StreamWriter sw = new StreamWriter(path);
            foreach (student stu in list)
            {
                sw.WriteLine(stu);
            }
            sw.Flush();
        }
    }
}

student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 读写
{
    class student : IComparable<student>
    {
        public int id;
        public string name;
        public string course;
        public int score;
        public student(int id, string name, string course,int score)
        {
            this.id = id;
            this.name = name;
            this.course = course;
            this.score = score;
        }
        public int CompareTo(student other)
        {
            return this.score > other.score ? -1 : 1;
        }
        public override string ToString()
        {
            return ""+id+" "+name+" "+course+" "+score;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值