1.读写txt的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 读写txt
{
internal class Program
{
private const string FILE_NAME = "Test.txt";
static void Main(string[] args)
{
WriteValues();
DisplayValues();
}
public static void DisplayValues()
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0}不存在", FILE_NAME);
Console.ReadLine();
return;
}
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);//新建文件流
StreamReader r = new StreamReader(fs);//通过阅读工具读出
for (int i = 0; i < 4; i++)
{
Console.WriteLine(r.ReadLine());
}
r.Close();
fs.Close();
Console.ReadLine();
}
public static void WriteValues()
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0}已存在", FILE_NAME);
Console.ReadLine();
return ;
}
FileStream fs=new FileStream(FILE_NAME, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter w = new StreamWriter(fs);
for(int i = 0; i < 4; i++)
{
w.WriteLine("Access");
}
w.Close();
fs.Close();
}
}
2.读写xml的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace 读写xml
{
public class RWXml
{
//生成一个xml文档
public void WriteXml()
{
//random
Random rd = new Random();
//创建xml文档
XmlDocument xDoc = new XmlDocument();
XmlDeclaration declaration=xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xDoc.AppendChild(declaration);//添加xml声明
XmlElement elem=xDoc.CreateElement("students");//创建根节点students
xDoc.AppendChild(elem);
for (int i = 1; i <= 10; i++)
{
XmlElement elem1 = xDoc.CreateElement("student");
elem.AppendChild(elem1);//添加子节点
elem1.SetAttribute("姓名", "张三");//添加属性
elem1.SetAttribute("学号", i.ToString());
XmlElement elem1_1 = xDoc.CreateElement("语文成绩");
elem1.AppendChild(elem1_1);
elem1_1.InnerText = rd.Next(50,90).ToString();
XmlElement elem1_2 = xDoc.CreateElement("数学成绩");
elem1.AppendChild(elem1_2);
elem1_2.InnerText = rd.Next(30,100).ToString();
XmlElement elem1_3 = xDoc.CreateElement("英语成绩");
elem1.AppendChild(elem1_3);
elem1_3.InnerText = rd.Next(50,95).ToString();
}
xDoc.Save("student.xml");
}
//读取
public void ReadXml()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("student.xml");
XmlNode node=xDoc.SelectSingleNode("students");
XmlNodeList nodeList=node.ChildNodes;//取子节点集合
foreach(XmlNode xn in nodeList)
{
//类型显示转换,取属性
XmlElement xmle=(XmlElement)xn;
string name = xmle.GetAttribute("姓名");
string no = xmle.GetAttribute("学号");
Console.WriteLine("姓名:"+name+"\n"+"学号:"+no);
XmlNodeList ChildList = xn.ChildNodes;//取子节点的子节点集合
foreach(XmlNode n in ChildList)
{
XmlElement xe = (XmlElement)n;
string classname = xe.Name;//标签名
string score=xe.InnerText;//内部文本
Console.WriteLine(classname+":"+score);
if (no == "10" && classname == "数学成绩")
{
xe.InnerText="100";//修改内部文本
}
}
}
xDoc.Save("student.xml");//若有修改记得保存
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 读写xml
{
internal class Program
{
static void Main(string[] args)
{
RWXml xml = new RWXml();
//xml.WriteXml();
xml.ReadXml();
}
}
}