最近由于考研复试需要,记录与总结一下自己的学习所得。
1.读入一个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>
预期输出结果为:
2019001,张三,机器学习,85
2019002,李四,操作系统,90
2019003,王五,数据结构,95
按照我自己的理解,xml是一个类似树状的文件结构,存在根节点,子节点等。可利用C#中已有API对各节点进行操作。下述代码将对XML的读、写及相应输出操作进行了封装。
using System.Xml;
public class RW
{
//生产xml文档
public void writexml()
{
//<?xml version="1.0" encoding="utf-8" ?>
//1.实例化一个xmlDocument类
XmlDocument xdoc = new XmlDocument();
//声明语法
XmlDeclaration xmlDeclaration = xdoc.CreateXmlDeclaration("1.0","utf-8","yes");
xdoc.AppendChild(xmlDeclaration);
//一个xml文档,必须要有根元素
//创建根节点grades
XmlElement xmlElement = xdoc.CreateElement("grades");
//把根节点添加到文档中去
xdoc.AppendChild(xmlElement);
create_child("2019001", "张三", "机器学习", 85, xdoc, xmlElement);
create_child("2019002", "李四", "操作系统", 90, xdoc, xmlElement);
create_child("2019003", "王五", "数据结构", 95, xdoc, xmlElement);
create_child("2019003","王五","数据结构","95",xdoc, xmlElement);
xdoc.Save("grades.xml");
}
//将子节点创建进行封装 -- 可根据实际需求进行修改
private void create_child(string id,string name,string course,int
score,XmlDocument xdoc, XmlElement xmlElement)
{
//添加子节点
XmlElement xmlElement3 = xdoc.CreateElement("grade");
/*
给节点添加属性:
如给grade添加:
xmlElement3.SetAttribute("Index","1");
*/
//把子节点放置根节点下
xmlElement.AppendChild(xmlElement3);
//子节点的子节点
XmlElement xmlElement3_1 = xdoc.CreateElement("id");
xmlElement3.AppendChild(xmlElement3_1);
xmlElement3_1.InnerText = id;
XmlElement xmlElement3_2 = xdoc.CreateElement("name");
xmlElement3.AppendChild(xmlElement3_2);
xmlElement3_2.InnerText = name;
XmlElement xmlElement3_3 = xdoc.CreateElement("course");
xmlElement3.AppendChild(xmlElement3_3);
xmlElement3_3.InnerText = course;
XmlElement xmlElement3_4 = xdoc.CreateElement("score");
xmlElement3.AppendChild(xmlElement3_4);
//INT转String
xmlElement3_4.InnerText = Convert.ToString(score);
//string转int用:Convert.ToInt32(string s)
//这个方法的返回值是int类型,要用int类型的变量接收
}
//读取并进行输出
public void readxml()
{
string txt = "";
XmlDocument xdoc = new XmlDocument();
//加载
xdoc.Load("grades.xml");
//获取根节点
XmlNode xmlNode = xdoc.SelectSingleNode("grades");
//获取节点的所有子节点
XmlNodeList xmlNodeList = xmlNode.ChildNodes;
foreach (XmlNode xn in xmlNodeList)
{
int count = 0;
//类型显示转化
//节点属性
/*
XmlElement xmle = (XmlElement)xn;
string id = xmle.GetAttribute("id");
string name = xmle.GetAttribute("name");
string course = xmle.GetAttribute("course");
string score = xmle.GetAttribute("score");
Console.WriteLine(id + "-"+ name + "-" + course + "-" + score);
*/
string id = "", name = "", course = "", score ="";
//取子节点属性
XmlElement xmle = (XmlElement)xn;
XmlNodeList xmlNodeList_child = xn.ChildNodes;
foreach (XmlNode xn_child in xmlNodeList_child)
{
//类型显示转化
XmlElement xmle_child = (XmlElement)xn_child;
switch (count)
{
case 0: id = xmle_child.InnerText; Console.WriteLine(id); break;
case 1: name = xmle_child.InnerText; Console.WriteLine(name); break;
case 2: course = xmle_child.InnerText; Console.WriteLine(course);
break;
case 3: score = xmle_child.InnerText; Console.WriteLine(score);
break;
}
count++;
}
// 下述为将xml按行写入txt文件与数据库
if(count == 4)
{
//db的创建参考学习笔记【1】中的sql_operate类
db.setbysql(@"insert into [grades] values ('"+ id + "','" + name + "','"
+ course + "','" + score+"') ");
txt += id + ","+ name + "," + course + "," + score + "\r\n";
Console.WriteLine(txt);
}
}
System.IO.File.WriteAllText(@"路径名\grades1.txt",txt);
Console.ReadKey();
}
}
2.读入一个txt类型的文件,并将每行读取信息以某一标点符号进行分割,以逗号为例。
using System.IO;
public void read_txt(string path)
{
String line;
try
{
//将文件路径名传入StreamReader
StreamReader sr = new StreamReader(path);
//读取第一行
line = sr.ReadLine();
//持续读取直至文件已经遍历完毕
while (line != null)
{
//利用Split进行字符串分割
string[] words = line.Split(',');
//将一条字符串分割后的数据在控制台进行输出
foreach (var word in words)
{
System.Console.WriteLine(word);
}
// System.Console.WriteLine("next");
//Read the next line
line = sr.ReadLine();
}
//System.Console.WriteLine("close");
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
3.读入一个txt文件,后以一定格式输出为xml文件;
txt文件内容为:
201833,王二,操作系统,70
201834,张三,操作系统,90
201834,张三,嵌入式系统,20
输出格式为:(名字重复需合并课程名,若出现重复名字将会是连续出现)
<students>
<student>
<学号>学生学号</学号>
<姓名>学生姓名</姓名>
<课程 课程名="课程名1">分数1</课程>
<课程 课程名="课程名2">分数2</课程>
</student>
</students>
实现代码如下(在1,2代码框架上修改实现)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
private XmlElement create_child_2(string id, string name, string course, string
score, XmlDocument xdoc, XmlElement xmlElement)
{
//添加子节点
XmlElement xmlElement3 = xdoc.CreateElement("student");
//xmlElement3.SetAttribute("Index", "1");
//把子节点放置根节点下
xmlElement.AppendChild(xmlElement3);
//子节点的子节点
XmlElement xmlElement3_1 = xdoc.CreateElement("学号");
xmlElement3.AppendChild(xmlElement3_1);
xmlElement3_1.InnerText = id;
XmlElement xmlElement3_2 = xdoc.CreateElement("姓名");
xmlElement3.AppendChild(xmlElement3_2);
xmlElement3_2.InnerText = name;
XmlElement xmlElement3_3 = xdoc.CreateElement("课程");
xmlElement3_3.SetAttribute("课程名", course);
xmlElement3.AppendChild(xmlElement3_3);
xmlElement3_3.InnerText = score;
return xmlElement3;
}
private void add_child_message(XmlDocument xdoc, XmlElement
xmlelement_temp, string course, string score)
{
XmlElement xmlElement3_3 = xdoc.CreateElement("课程");
xmlElement3_3.SetAttribute("课程名", course);
xmlelement_temp.AppendChild(xmlElement3_3);
xmlElement3_3.InnerText = score;
}
public void txt_to_xml(string path1,string path2)
{
String line;
String name="";//存储上一行中的学生名字信息
//<?xml version="1.0" encoding="utf-8" ?>
//1.实例化一个xmlDocument类
XmlDocument xdoc = new XmlDocument();
//存储xml最后一个子节点
XmlElement xmlelement_temp = null;
//声明语法
XmlDeclaration xmlDeclaration = xdoc.CreateXmlDeclaration("1.0",
"utf-8", "yes");
xdoc.AppendChild(xmlDeclaration);
//一个xml文档,必须要有根元素
//创建根节点grades
XmlElement xmlElement = xdoc.CreateElement("students");
//把根节点添加到文档中去
xdoc.AppendChild(xmlElement);
try
{
//Pass the file path and file name to the StreamReader
constructor
StreamReader sr = new StreamReader(path1);
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
int length=0;
string[] words = line.Split(',');
//write the line to console window
foreach (var word in words)
{
length++;
}
if (!name.Equals(words[1]))
{
name = words[1];
xmlelement_temp = create_child_2(words[0], words[1],
words[2], words[3], xdoc, xmlElement);
}
else
{
add_child_message(xdoc, xmlelement_temp, words[2],
words[3]);
System.Console.WriteLine("equal");
}
// System.Console.WriteLine("next");
//Read the next line
line = sr.ReadLine();
}
xdoc.Save(path2);
//System.Console.WriteLine("close");
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}