控制台项目(像c++)
Grade.cs(模型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace txttoxml1
{
[XmlRoot("grades")]
public class Grade
{
public string Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
public int Score { get; set; }
public string LENGTH { get; set; }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml;
namespace txttoxml1
{
public class Program
{
static void Main(string[] args)
{
// var path = AppDomain.CurrentDomain.BaseDirectory + "\\" + "output.txt";
var path = @"D:\\softwaretools\\vs2022\\sourse\\xmltxtresource\\input1.txt";
StreamReader sr = new StreamReader(path, Encoding.UTF8);
String line;
//一行一行读取转化list集合
List<Grade> gradeList = new List<Grade>();
while ((line = sr.ReadLine()) != null)//文本未读完
{
var str= line.ToString().Split(',');
gradeList.Add(new Grade
{
Id = str[0],
Name = str[1],
Course = str[2],
Score = Convert.ToInt32(str[3]),
LENGTH = str[4]
});
}
// 按照成绩由大到小排序
gradeList.Sort((x, y) => y.Score.CompareTo(x.Score));
//转为xml并保存
var strXml = XmlSerialize(gradeList, Encoding.UTF8);
// 将成绩信息输出到txt文件
// var outPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + "output2.txt";
var outPath = @"D:\\softwaretools\\vs2022\\sourse\\xmltxtresource\\output1.xml";
using (StreamWriter file = new StreamWriter(outPath))
{
file.WriteLine(strXml);
}
Console.WriteLine("转换已完成2");
}
/// <summary>
/// 将一个对象序列化为XML字符串XmlSerialize
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>序列化产生的XML字符串</returns>
public static string XmlSerialize(object o, Encoding encoding)
{
if (o == null)
{
throw new ArgumentNullException("o");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
using (MemoryStream stream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = " ";
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
//Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
//Add an empty namespace and empty value
ns.Add("", "");
serializer.Serialize(writer, o, ns);
writer.Close();
}
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
}
}
input1.txt
注意为中文逗号:
2019003,王五,数据结构,95 ,185
2019002,李四,操作系统,90 ,188
2019001,张三,机器学习,85 ,166
2019010,赵六六,数学分析,100,175
输出到
输出效果:
<ArrayOfGrade>
<Grade>
<Id>2019010</Id>
<Name>赵六六</Name>
<Course>数学分析</Course>
<Score>100</Score>
<LENGTH>175</LENGTH>
</Grade>
<Grade>
<Id>2019003</Id>
<Name>王五</Name>
<Course>数据结构</Course>
<Score>95</Score>
<LENGTH>185</LENGTH>
</Grade>
<Grade>
<Id>2019002</Id>
<Name>李四</Name>
<Course>操作系统</Course>
<Score>90</Score>
<LENGTH>188</LENGTH>
</Grade>
<Grade>
<Id>2019001</Id>
<Name>张三</Name>
<Course>机器学习</Course>
<Score>85</Score>
<LENGTH>166</LENGTH>
</Grade>
</ArrayOfGrade>