天天看點

C#:讀入xml 寫入 txt;實作xml排序再寫入txt

xml

<?xml version="1.0" encoding="utf-8"?>
<grades>
    <grade>
       <id>2019001</id>
       <name>張三</name>
       <course>機器學習</course>
       <score>85</score>
    </grade>
    <grade>
       <id>2019002</id>
       <name>李四</name>
       <course>作業系統</course>
       <score>90</score>
    </grade>
	<grade>
       <id>2019003</id>
       <name>王五</name>
       <course>資料結構</course>
       <score>95</score>
    </grade>
</grades>
           

C#讀入并寫進txt檔案

方法一:利用(推薦方法二)

XDocument

,

XElement

,

IEnumerable<XElement>

,

.Name

,

.Value

FileInfo

,

StreamWriter

,

1.讀入xml檔案
XDocument document = XDocument.Load("D:/code/info.xml");
  建立txt檔案
string path = "D:/Code/info.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists) fi.Create().Close();
StreamWriter sw = new StreamWriter(path);
            
2.擷取xml根元素 以及其中第一個grade元素
XElement root = document.Root;
           

擷取元素名稱:

XElement ele = root.Element("grade");
3.先擷取其中子标簽的名稱寫到txt中
string str = "";
foreach(XElement e in ele.Elements())
{
    str+=e.Name+" ";
}
sw.WriteLine(str);
           
4.擷取所有的子元素
IEnumerable<XElement> enumerable = root.Elements();

5.所有元素寫到txt中
foreach(XElement item in enumerable)
{
    str = "";
    foreach(XElement it in item.Elements())
    {
        str += it.Value + " ";
    }
    sw.WriteLine(str);
}
sw.Close();
           

方法二:利用

XmlDocument

XmlNodeList

,

XmlNode

XmlDocument xd = new XmlDocument();
xd.Load(@"D:/code/info.xml");

string path = "D:/Code/info.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists) fi.Create().Close();
StreamWriter sw = new StreamWriter(path);

XmlNodeList list = xd.DocumentElement.ChildNodes;

foreach(XmlNode xn in list)
{
    string id = xn.ChildNodes[0].InnerText;
    string name = xn.ChildNodes[1].InnerText;
    Debug.WriteLine(id + " * " + id2);
}
           

加入排序

List<T>

IComparable<T>

接口或者定義一個實作

IComparaer<T>

的比較器

class Student 類
class Student : IComparable<Student>
    {
	     private int id;
	     private string name;
	     private string course;
	     private int score;
	
	     public int Id { get => id; set => id = value; }
	     public string Name { get => name; set => name = value; }
	     public string Course { get => course; set => course = value; }
	     public int Score { get => score; set => score = value; }
	
	     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 name + ",score=" + score;
	     }
	 }
           
實作函數
XmlDocument xd = new XmlDocument();
xd.Load(@"D:/code/info.xml");
string path = "D:/Code/info.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists) fi.Create().Close();
StreamWriter sw = new StreamWriter(path);

XmlNodeList list = xd.DocumentElement.ChildNodes;
List<Student> slist = new List<Student>();
foreach(XmlNode xn in list)
{
    int id = int.Parse(xn.ChildNodes[0].InnerText);
    string name = xn.ChildNodes[1].InnerText;
    string course = xn.ChildNodes[2].InnerText;
    int score = int.Parse(xn.ChildNodes[3].InnerText);
    slist.Add(new Student(id, name, course, score));
}
slist.Sort();
foreach(Student stu in slist)
{
    sw.WriteLine(stu);
}
sw.Close();
           
C#:讀入xml 寫入 txt;實作xml排序再寫入txt

繼續閱讀