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;
}
}
}